簡體   English   中英

從最近的應用程序中刪除應用程序后,IntentService停止工作

[英]IntentService stops working when app is removed from recent apps

我在應用程序中使用了地理圍欄,並且基於地理圍欄事件(Enter或Exit),我想執行一些操作。 Geofence 文檔說,一旦設置了geofence,它將自動觸發事件,您可以使用IntentService捕獲此事件。 為此,我進行了如下intentservice:

GeofenceTransitionsIntentService.java

public class GeofenceTransitionsIntentService extends IntentService {

    Handler mHandler;
    public GeofenceTransitionsIntentService() {
        super("GeofenceTransitionsIntentService");
        mHandler = new Handler();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("JK-->>","service started!");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Log.e("JK-->>","onHandel--->>");

        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        if (geofencingEvent.hasError()) {
            Log.e("JK-->>","geofenceEvent has error!");
            return;
        }

        int geofenceTransitionType = geofencingEvent.getGeofenceTransition();
        if (geofenceTransitionType == Geofence.GEOFENCE_TRANSITION_ENTER) {
            Log.e("JK-->>","enter!");
            mHandler.post(new DisplayToast(this,"Enter"));
        } else if (geofenceTransitionType == Geofence.GEOFENCE_TRANSITION_EXIT) {
            mHandler.post(new DisplayToast(this,"Exit"));
            Log.e("JK-->>","exit");
        }
    }

    public class DisplayToast implements Runnable {
        private final Context mContext;
        String mText;

        public DisplayToast(Context mContext, String text){
            this.mContext = mContext;
            mText = text;
        }

        public void run(){
            Toast.makeText(mContext, mText, Toast.LENGTH_SHORT).show();
        }
    }

}

現在,問題是,當應用程序打開時(無論前景還是背景),並且我在地理圍欄中進入或退出時,它都可以正常工作,並向我顯示一條吐司消息,logcat顯示日志,但是當我從最近的應用程序中刪除該應用程序時,沒有吐司消息顯示對我來說還是logcat中沒有日志顯示。

我試圖在Google上找到解決方案,但是大多數答案都建議使用該服務,但是如果我沒記錯,那么IntentService在工作完成后會自動停止,並在收到任何意圖時自動啟動。 因此,我認為使用IntentService來完成此任務更為有效。

更新我正在使用以下代碼行注冊geofence。

geofencingClient.addGeofences(getGeofencingRequest(),getGeofencePendingIntent());

在getGeofencePendingIntent()中,我正在使用以下代碼行啟動意圖服務。

private PendingIntent getGeofencePendingIntent() {
        if(geofencePendingIntent != null)
            return geofencePendingIntent;
        Intent in = new Intent(SetProfileOnlineActivity.this,GeofenceTransitionsIntentService.class);
        geofencePendingIntent = PendingIntent.getService(SetProfileOnlineActivity.this,111451,in,PendingIntent.FLAG_UPDATE_CURRENT);
        return geofencePendingIntent;
    }

該服務將始終運行:


轉到項目Java->右鍵單擊->新建->服務->將其命名為watchman


watchman.java

public class watchman extends Service
{
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
NotificationChannel notificationChannel;
String NOTIFICATION_CHANNEL_ID = "1";


public watchman() { }

@Override
public void onCreate()
{

    try
    {
        mNotifyManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);

        mBuilder = new NotificationCompat.Builder(this, null);
        mBuilder.setContentTitle("Insta Promo")
                .setContentText("We are ready to help you.")
                .setSmallIcon(R.drawable.ic_launcher_background);



        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);

            // Configure the notification channel.
            notificationChannel.setDescription("Channel description");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            mNotifyManager.createNotificationChannel(notificationChannel);
        }
        else
        {
            mBuilder.setContentTitle("Insta Promo")
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setColor(ContextCompat.getColor(this, R.color.colorAccent))
                    .setVibrate(new long[]{100, 250})
                    .setLights(Color.YELLOW, 500, 5000)
                    .setAutoCancel(true);
        }

        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);

        mNotifyManager.notify(1, mBuilder.build());

        startForeground(1, mBuilder.build());

    }
    catch(Exception e)
    {
        Log.d(TAG, "EXCEPTION IN SHOWING NOTIFICATION xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...\n");
        Log.e("MY_APP", "exception", e);

    }

}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{

    new Thread(new Runnable()
    {

        public void run()
        {

            while (true)
            {
                try
                {
                    Log.d(TAG, "Thread : Running again...\n");

                    Thread.sleep(10000);
                }
                catch (InterruptedException e)
                {
                    Log.d(TAG, "Thread : InterruptedException Error in service...\n");
                }

            }


        }

    }).start();
    return START_STICKY;

}

@Override
public void onDestroy()
{
    super.onDestroy();
}

@Override
public IBinder onBind(Intent intent)
{
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

}

當您將其創建為服務時,它將自動注冊到清單文件中,而無需更新清單文件。


從主要活動或您想開始的任何地方開始

    Log.d(TAG, " Good to Go \n");
    Log.d(TAG, "Starting Service from main...\n");
    Intent intent = new Intent(MainActivity.this, watchman.class);
    startService(intent);
    Log.d(TAG, "Main has started the service...\n");

現在,即使將其從最近的記錄中刪除...,它也將始終在內存中為您運行,要檢查它,請密切注意logcat。 希望能幫助到你。 從4.1開始到最新的8.0 oreo的項目工作


用於顯示通知,我正在使用振動許可,因此還可以使清單文件可用。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rushi.oreo">


<uses-permission android:name="android.permission.VIBRATE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".watchman"
        android:enabled="true"
        android:exported="true" />


</application>

</manifest>

希望它確實對您或其他人有幫助。

  1. 當分配給它的工作完成時,IntentService將自動停止。
  2. 如果您希望某項服務在后台運行且停止的機會很小,則它必須是“前台服務”。 請確保在后台工作線程中啟動服務,因為默認情況下,服務在主線程上運行。

更多詳細信息在這里-https : //developer.android.com/reference/android/app/Service.html#startForeground(int,android.app.Notification)

但是請注意 ,將服務設為前台會嚴重影響手機的電池壽命。 而且,作為前台進行服務也使用戶感到煩惱,因為它始終顯示通知並且無法關閉。

您可以更好地使用JobScheduler或Firebase JobDispatcher安排后台工作。

我找到了答案...我的代碼沒有問題,IntentService也運行得很好,但是錯誤出在測試中 我正在android Oreo運行設備上測試我的應用程序。

在android oreo中,google更新了其政策,即在前台,他們將發送位置更新任意次,而在后台,他們將在幾小時內僅發送幾次位置更新。 其背后的主要原因是為了節省設備的使用壽命。

有關android oreo位置更新的更多信息,請查看文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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