我希望我的服务和广播接收器 运行 即使应用程序已从应用程序列表中清除或被迫停止
I want my service and broadcast receiver to run even if app is cleared from app list or forced stop
我希望我的服务和广播接收器在后台 运行 即使应用已关闭或强制停止。
我的接收器正在检测屏幕响应并执行任务。
当应用程序打开时以及应用程序在应用程序列表中但未关闭时,它工作正常。
当我从应用程序列表中清除该应用程序时,它没有检测到屏幕响应。
我尝试了很多方法,但都没有成功。帮帮我。
清单文件:-
<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>
我的服务class:-
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 class:-
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")
}
}
}
您可以使用服务
在主应用程序中 start/stop 服务
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);
}
}
我希望我的服务和广播接收器在后台 运行 即使应用已关闭或强制停止。
我的接收器正在检测屏幕响应并执行任务。 当应用程序打开时以及应用程序在应用程序列表中但未关闭时,它工作正常。 当我从应用程序列表中清除该应用程序时,它没有检测到屏幕响应。 我尝试了很多方法,但都没有成功。帮帮我。
清单文件:-
<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>
我的服务class:-
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 class:-
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")
}
}
}
您可以使用服务 在主应用程序中 start/stop 服务
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);
}
}