【问题标题】:How to keep service alive when remove app from stack从堆栈中删除应用程序时如何保持服务活动
【发布时间】:2017-08-24 22:23:51
【问题描述】:

我想使用警报管理器每分钟显示一次通知,我已经实现了下面的代码,它工作正常,但问题是当我从堆栈中删除应用程序时,服务没有运行。

我想保持活力,我在 onStartCommand 中尝试过 START_STICKY,也使用过 onTaskRemoved,但还是一样。

我也尝试使用 WakefulIntentService 来实现,但问题是一样的。我的代码如下。

在主活动中

    Intent myIntent = new Intent(NotificationDemo.this, MyReceiver.class);

                myIntent.putExtra("title", "2 minutes");

                Random random = new Random();
                int m = random.nextInt(9999 - 1000) + 1000;

                Log.d("m::: In Notification", m + "");
                myIntent.putExtra("id", m);

                pendingIntent = PendingIntent.getBroadcast(NotificationDemo.this, m, myIntent, 0);

                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//                alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),alarmManager.Inte pendingIntent);
                alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        SystemClock.elapsedRealtime(),
                        1 * 60 * 1000,
                        pendingIntent);

我的服务

       public class MyAlarmService extends Service {

            private NotificationManager mManager;

            @Override
            public IBinder onBind(Intent arg0) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public void onCreate() {
                // TODO Auto-generated method stub
                super.onCreate();
            }


            @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
        String title = intent.getStringExtra("title");
        int id = intent.getIntExtra("id", 0);
        mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
        Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);

        Notification notification = new Notification(R.mipmap.ic_launcher, title, System.currentTimeMillis());
        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        Log.d("id::", id + "");

        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), id, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
//        notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(MyAlarmService.this);

        notification = builder.setContentTitle(title)
                .setContentText("Hellooo...")
                .setTicker("Good Evening...")
                .setSmallIcon(android.R.drawable.ic_btn_speak_now)
                .setVibrate(new long[]{1000, 1000, 1000, 100})
                .setLights(5, 5, 5)
                .setContentIntent(pendingNotificationIntent).build();

        mManager.notify(id, notification);
        startForeground(1337, notification);
        return START_STICKY;
    }

            @Override
            public void onDestroy() {
                // TODO Auto-generated method stub
                super.onDestroy();
               // sendBroadcast(new Intent("IWillStartAuto"));
            }

            @Override
            public void onTaskRemoved(Intent rootIntent) {
                super.onTaskRemoved(rootIntent);
        //        sendBroadcast(new Intent("IWillStartAuto"));
        //        Intent intent = new Intent(getApplicationContext(),MyReceiver.class);
        //        sendBroadcast(intent);
            }
        }

这是我的接收器

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
         String title = intent.getStringExtra("title");
    int id = intent.getIntExtra("id", 0);
    Intent service1 = new Intent(context, MyAlarmService.class);
    service1.putExtra("title", title);
    service1.putExtra("id", id);
    context.startService(service1);

    }
}

在清单中

<receiver android:name=".MyReceiver">
        <!--<intent-filter>
            <action android:name="IWillStartAuto"/>
        </intent-filter>-->
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <service
        android:name=".MyAlarmService"
        android:enabled="true"
        android:stopWithTask="false" />

【问题讨论】:

    标签: android service broadcastreceiver alarmmanager


    【解决方案1】:

    您需要在前台启动您的服务。
    您的服务 OnCreate() 方法中缺少此内容。

    startForeground(1337, notification);
    return START_STICKY;
    

    并且不要在您的活动或片段 onDestroy() 方法中停止您的服务。

    【讨论】:

    • 它在其他设备和模拟器中工作,但在我的设备中不工作Vivo y31L
    • @Komal 你能在那个设备上安装这个工具并告诉我使用 RAM 百分比吗? play.google.com/store/apps/…
    • 好的,让我安装这个应用程序!
    • 是 25% - 27%。
    • 问题是:我的设备中缺少设置转到 iManager>App Manager>Autostart Manager .. 然后选择您希望通知显示自动启动的应用程序..
    【解决方案2】:

    我在装有 android 6.0 的手机和装有 android 7.0 的模拟器上测试了这段代码,两者都运行良好,甚至从堆栈中删除了应用程序。
    即使删除接收者代码,通知仍然有效。

    【讨论】:

    • 我已经在模拟器中签入它工作正常,但在我的手机服务从堆栈中删除应用程序后停止!
    【解决方案3】:

    您可以尝试使用接收器配置 process=":remote" 来保持接收器处于活动状态。我遇到了这个问题,这是我的解决方案。

    【讨论】:

      【解决方案4】:

      您是否尝试过将代码与任何其他输出语句一起使用?通知也迟到了,也没有被我解雇-我可能会责怪 Instant Run,但是您可以尝试用 Toast 之类的东西替换通知触发代码 sn-p 吗?由于 Toast 在服务中不起作用,也许您可​​以尝试在服务开始知道它可能已启动并运行时在您的外部存储目录中创建一个文件?

      【讨论】:

        猜你喜欢
        • 2013-08-11
        • 2018-08-29
        • 1970-01-01
        • 2010-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多