从最近的应用程序中删除应用程序时,IntentService停止工作

问题描述 投票:0回答:3

我在我的应用程序中使用地理围栏并基于地理围栏事件(Enter或Exit)我想执行一些操作。地理围栏documentation说,一旦你设置地理围栏,它将自动触发事件,你可以用IntentService捕获这些事件。为此我做了如下的意图服务:

Geo fence transitions intent service.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();
        }
    }

}

现在,问题是,当应用程序打开时(无论前景或背景)我进入或退出地理围栏它工作正常并向我显示一个toast消息和logcat显示日志但是当我从最近的应用程序中删除应用程序时没有显示toast消息对我或没有日志显示在logcat中。

我试图在谷歌上找到解决方案,但大多数答案建议使用该服务,但如果我没有错,那么IntentService在工作完成后自动停止并在收到任何意图时自动启动。因此,我认为使用IntentService执行此任务更有效。

更新我正在使用以下代码行注册地理围栏。

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

在getGeofencePendingIntent()中,我使用以下代码行启动intent服务。

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 android intentservice android-geofence
3个回答
0
投票

此服务将始终运行:


转到项目java - >右键单击 - >新建 - >服务 - >服务名称看守员


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奥托


用于显示通知我正在使用振动权限,因此也可以为您提供清单文件。

<?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>

希望它真的能帮助你或其他人。


0
投票
  1. 当分配给它的工作完成后,IntentService将自动停止。
  2. 如果您希望服务在后台运行且停止的可能性很小,则必须是前台服务。请确保在后台工作线程中启动您的服务,因为默认情况下,服务在主线程上运行。

更多细节在这里 - https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)

但请注意,将服务作为前台会影响手机的电池续航时间。并且将服务作为前景也对用户来说是烦人的,因为它始终显示通知并且不能被关闭。

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


0
投票

我找到了答案......我的代码没有问题,IntentService也运行正常,但错误在于测试。我在android Oreo运行设备上测试我的应用程序。

在Android oreo谷歌已经更新了他们的政策,在前台他们将发送任意次数的位置更新,但在后台,他们将在一小时内发送位置更新几次。它背后的主要原因是拯救设备的赌博生活。

有关android oreo位置更新的更多信息,您可以查看this文档。

© www.soinside.com 2019 - 2024. All rights reserved.