簡體   English   中英

如何在android studio java中通過api 26殺死應用程序后創建始終運行的服務檢測調用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM