Android的服务:START_STICKY不能在奇巧工作奇巧、能在、工作、Android

2023-09-05 06:03:45 作者:花为霓裳

我正在使用的应用程序服务,听多少次用户presses他/她的电源按钮。执行工作正常的所有​​设备。但是,当我测试对Android奇巧的应用程序,我发现了一些错误。

I am using services in an application to listen for how many times the user presses his/her power button. The implementation was working fine on all devices. But when I tested the app on the Android Kitkat, I noticed something wrong.

当我刷卡应用离最近通话应用程序,应用程序不再侦听电源按钮。

As soon as I swipe the application away from the recents apps, the application no longer listens for the power button.

下面是code,我有工作:

Here is the code that I am working with:

public class Receiver extends Service {

    Notification notification;
    private static final int NOTIFICATION_ID = 0;
    NotificationManager manager;
    PendingIntent toOpen;
    Intent intent;
    private BroadcastReceiver POWER_BUTTON = new Powerbuttonrecceiver();

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

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

        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(POWER_BUTTON, filter);
        startNotify();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        unregisterReceiver(POWER_BUTTON);
        dismissNotification();
        super.onDestroy();
    }

    public void startNotify(){
        manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        int icon = R.drawable.ic_launcher_drop;
        CharSequence tickerText = "Service activated";
        CharSequence tickerContent = "Service is now on. You can press your power button and the app will listen to it. Tap to turn this feature off";
        intent = new Intent(Receiver.this, Options.class);
        toOpen = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

        notification = new NotificationCompat.Builder(getApplicationContext())
        .setContentTitle(tickerText)
        .setContentText(tickerContent)
        .setSmallIcon(icon)
        .setOngoing(true)
        .setContentIntent(toOpen)
        .build();
        manager.notify(NOTIFICATION_ID, notification);
    }

    public void dismissNotification(){
        manager.cancel(NOTIFICATION_ID);
    }

}

正如人们可以看到我使用一个通知,表示该服务处于活动状态。这混淆了我的事情是,后从最近通话轻扫APP通知仍然存在,什么是无效的是的BroadcastReceiver ?或者是假的。

在应用程序的的onDestroy 我没有叫任何函数注册或注销,以及停止该服务。再次,这个问题只出现在Android的奇巧。请,如果你们知道发生了什么。请帮助:)

In the app's onDestroy I have not called in any functions to register or unregister as well as stop the service. Once again, this problem is only seen in Android KitKat. Please if you guys know whats happening. Do help :)

更新:我也注意到与奇巧的播放音乐,当我刷它离最近通话应用程序的音乐停止。这是奇巧一个错误?但在声音/媒体播放服务 SoundCloud 从最近通话应用程序拨弄了一下,即使工作。

UPDATE: I also noticed with the Play Music on Kitkat, when I swipe it away from the recents app the music stops. Is this a bug on Kitkat? But the sound/mediaplayer services on SoundCloud works even when swiped away from the recents app.

更新: 登录为发行63618 在Android问题跟踪。 阅读问题的意见的更多细节。

UPDATE: Logged as Issue 63618 on android issue tracker. Read issue comments for more details.

推荐答案

看来,这是一个错误present在 Android 4.4系统,得到周围用下面的

Seems that this is a bug present in Android 4.4, got around it with the following:

@Override
public void onTaskRemoved(Intent rootIntent) {
    // TODO Auto-generated method stub
    Intent restartService = new Intent(getApplicationContext(),
            this.getClass());
    restartService.setPackage(getPackageName());
    PendingIntent restartServicePI = PendingIntent.getService(
            getApplicationContext(), 1, restartService,
            PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);

}

因此​​,这是在服务类本身覆盖的方法。我使用的原因 AlarmManager 是这样奇巧不杀我的服务。

So this is a method that is overridden in the Service class itself. The reason that I am using the AlarmManager is so that Kitkat does not kill my service.

 
精彩推荐