运用service、BroadcastReceiver保持app不被系统杀死

首先我简单介绍一下我们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,感谢提供者,等下周有空再做详细整理。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当 Android 应用程序被用户手动杀死时,应用程序收到 `onDestroy()` 生命周期回调函数。但是,在这种情况下,您无法阻止应用程序被杀死。因此,如果您希望在应用程序被杀死时执行某些操作,例如重新启动应用程序或发送通知等,您需要使用一些其他方法。 一种方法是使用 Android 的 `Service` 组件。通过创建一个 `Service`,您可以在应用程序被杀死后在后台继续运行代码。要监听应用程序被杀死的事件,您可以在 `Service` 中实现 `onTaskRemoved()` 方法。 在 `onTaskRemoved()` 方法中,您可以执行您希望在应用程序被杀死时执行的操作。例如,您可以使用 `PendingIntent` 发送一个通知,提示用户重新启动应用程序。 以下是一个示例代码片段,演示如何在 `Service` 中监听应用程序被杀死的事件: ```java public class MyService extends Service { @Override public void onCreate() { super.onCreate(); // 在此处执行任何必要的初始化操作 } @Override public int onStartCommand(Intent intent, int flags, int startId) { // 在此处执行您希望在后台运行的代码 return START_STICKY; } @Override public void onTaskRemoved(Intent rootIntent) { // 在此处执行您希望在应用程序被杀死时执行的操作 super.onTaskRemoved(rootIntent); } @Override public IBinder onBind(Intent intent) { // 如果您的 Service 不提供绑定,则返回 null return null; } } ``` 请注意,使用 `Service` 可能对设备的电池寿命产生影响。如果您希望在应用程序被杀死时执行某些操作,但不需要在后台运行代码,请考虑使用 Android 的 `AlarmManager` 和 `BroadcastReceiver` 组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值