繁体   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