我希望我的服务和广播接收器能够运行,即使应用程序从应用程序列表中清除或强制停止

时间:2018-03-15 07:40:10

标签: android service broadcastreceiver

即使应用已关闭或强制停止,我希望我的服务和广播接收器在后台运行。

我的接收器正在检测响应屏幕并执行任务。 当应用程序打开以及应用程序在应用程序列表中但未关闭时,它的工作正常。 当我从应用程序列表中清除应用程序时,它没有在响应时检测到屏幕。 我尝试了很多方法,但无法取得成功。帮助我。

清单文件: -

<receiver
        android:name=".broadcastReceiver.MyReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_OFF"/>
            <action android:name="android.intent.action.SCREEN_ON"/>
        </intent-filter>
    </receiver>

    <service
        android:name=".service.MyService"
        android:enabled="true"
        android:exported="true"
        android:stopWithTask="false">

    </service>

MyService类: -

private var myReceiver:MyReceiver?=null

private var defaultUEH:Thread.UncaughtExceptionHandler?=null
private var uncaughtExceptionHandler = object:Thread.UncaughtExceptionHandler{

    override fun uncaughtException(thread: Thread?, ex: Throwable?) {

        Log.d("Exception found","Uncaught exception start!")
        ex!!.printStackTrace()

        unregisterReceiver(myReceiver)

        var intent = Intent(applicationContext, MyService::class.java)
        intent.setPackage(packageName)
        var pendingIntent = PendingIntent.getService(applicationContext,1,intent,PendingIntent.FLAG_ONE_SHOT)
        var alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
        alarmManager.set(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime()+1000,pendingIntent)
        System.exit(2)

    }

}

override fun onBind(intent: Intent): IBinder? {
   return null
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

    var intentFilter = IntentFilter("android.intent.action.SCREEN_OFF")
    intentFilter.addAction("android.intent.action.SCREEN_ON")

    myReceiver = MyReceiver()
    registerReceiver(myReceiver , intentFilter)

    return START_STICKY
}

override fun onCreate() {
    super.onCreate()
    Log.d("Service" , "onCreate fun")
}

override fun onDestroy() {

    super.onDestroy()

    Log.d("Service" , "onDestroy fun")
}

override fun onTaskRemoved(rootIntent: Intent?) {

    unregisterReceiver(myReceiver)

    var intent = Intent(applicationContext, MyService::class.java)
    intent.setPackage(packageName)
    var pendingIntent = PendingIntent.getService(this,1,intent,PendingIntent.FLAG_ONE_SHOT)
    var alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    alarmManager.set(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime()+1000,pendingIntent)
    super.onTaskRemoved(rootIntent)

}

MyReceiver类: -

override fun onReceive(context: Context?, intent: Intent?) {

    Log.d("Where ","Broadcast")
    if (intent!!.action.equals(Intent.ACTION_SCREEN_ON)){

        var dashboardActivity = DashboardActivity.instance
        var isSosEnabled = dashboardActivity!!.getSos()

        if (isSosEnabled){
            Log.d("Service Sos Enabled","True")
        }else{
            Log.d("Service Sos Enabled", "False")
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您可以使用服务 在主应用程序中启动/停止服务

Intent service = new Intent(context, MyService.class);
context.startService(service);
...
Intent service = new Intent(context, MyService.class);
context.stopService(service);

<强>服务

public class MyService extends Service
{
 private static BroadcastReceiver m_ScreenOffReceiver;

 @Override
 public IBinder onBind(Intent arg0)
 {
  return null;
 }

 @Override
 public void onCreate()
 {
  registerScreenOffReceiver();
 }

 @Override
 public void onDestroy()
 {
  unregisterReceiver(m_ScreenOffReceiver);
  m_ScreenOffReceiver = null;
 }

 private void registerScreenOffReceiver()
 {
  m_ScreenOffReceiver = new BroadcastReceiver()
  {
   @Override
   public void onReceive(Context context, Intent intent)
   {
     Log.d(TAG, "ACTION_SCREEN_OFF");
     // do something, e.g. send Intent to main app
   }
  };
  IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
  registerReceiver(m_ScreenOffReceiver, filter);
 }
}