首先我简单介绍一下我们app要达到的目的,在我们app中操作指令从而实现在微信中加粉发朋友圈等功能,等指令运行完毕回到自己app的界面,继续查询指令,执行指令。从去年开始就一直用的线程,但是,就在近期发现每次操作指令在微信中操作时,我们的app被杀死了!对于我这个小菜鸟来说,简直成了个难题!然后在网上搜了很多资料,经过尝试,在小米手机中已经实现可以让自己的app不被杀死!
技术上的讲解免了,直接贴源码
service的使用,继承Service
public class MyService extends Service {
private String TAG="main";
public class LocalBinder extends Binder{
String stringToSend="I'm the test String";
MyService getsMyService(){
Log.i(TAG, "getService ---> " + MyService.this);
return MyService.this;
}
}
private final IBinder mBinder=new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind~~~~~~~~~~~~");
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate~~~~~~~~~~~");
MyReceiver searchReceiver = new MyReceiver();
// 动态注册广播
IntentFilter localIntentFilter = new IntentFilter(
"android.intent.action.USER_PRESENT");
localIntentFilter.addAction("listener");
registerReceiver(searchReceiver, localIntentFilter);
}
@SuppressWarnings("deprecation")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = new Notification(R.drawable.ic_launcher,
getString(R.string.app_name), System.currentTimeMillis());
PendingIntent pendingintent = PendingIntent.getActivity(this, 0,
new Intent(this, MainTabActivity.class), 0);
notification.setLatestEventInfo(this, "uploadservice", "请保持程序在后台运行",
pendingintent);
startForeground(0x11, notification);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Intent localIntent = new Intent();
localIntent.setAction("listener");
sendBroadcast(localIntent);
}
}
在清单文件中配置:<service android:name="service.MyService"
特别说明一点,启动前台服务
前台服务是被认为是用户已知的正在运行的服务,当系统需要释放内存时不会优先杀掉该进程。前台进程必须发一个 notification
在状态栏中显示,直到进程被杀死。因为前台服务会一直消耗一部分资源,但不像一般服务那样会在需要的时候被杀掉,所以为了能节约资源,保护电池寿命,一定要在建前台服务的时候发notification
,提示用户。当然,系统提供的方法就是必须有 notification
参数的,所以不要想着怎么把 notification
隐藏掉。
@SuppressWarnings("deprecation")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = new Notification(R.drawable.ic_launcher,
getString(R.string.app_name), System.currentTimeMillis());
PendingIntent pendingintent = PendingIntent.getActivity(this, 0,
new Intent(this, MainTabActivity.class), 0);
notification.setLatestEventInfo(this, "uploadservice", "请保持程序在后台运行",
pendingintent);
startForeground(0x11, notification);
return super.onStartCommand(intent, flags, startId);
}
startForeground() 方法就是将服务设为前台服务。参数12346就是这个通知唯一的id,只要不为0即可。
service.MyService是包名+类名
广播的使用,继承BroadcastReceiver
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
context.startService(new Intent(context, MyService.class));
}
清单文件中配置:
<receiver android:name="service.MyReceiver" >
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.media.RINGER_MODE_CHANGED"/>
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
<action android:name="com.dbjtech.waiqin.destroy" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
最后在主界面的oncreate()方法中启动service
Intent intent = new Intent(this, MyService.class);
startService(intent);
本文参考:http://www.tuicool.com/articles/iu22QnF,感谢提供者,等下周有空再做详细整理。