[英]How to create service Detaction call always running after killed app over api 26 in android studio java
我正在使用此代码,如果应用程序在屏幕中处于活动状态,则此代码正在运行,如果关闭,则代码未运行
如果应用程序被杀死,如何解决这个问题,就像 truecaller 应用程序在后台运行一样
package com.islamelwakeel.detectcall;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;
public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
showToast(context,"Call started...");
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)){
showToast(context,"Call ended...");
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)){
showToast(context,"Incoming call...");
Log.d("call","Incoming call...");
}
}
void showToast(Context context,String message){
Toast toast=Toast.makeText(context,message,Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
}
我的服务也被杀了。 我尝试了很多方法,但我发现了一种有效的方法。 我使用了一个接收系统调用的服务来检查我的服务是否仍在运行。 如果它不起作用,请启动它。 新服务。 例如, NotificationListenerService 将拥有它。 总会有一些事件发生。 我可以编写代码来重新启动我的服务
前任。
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
if(!isMyserviceRunning){
startService();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.