从后台删除应用程序时,广播接收器无法正常工作吗?

时间:2018-11-12 13:48:56

标签: java android android-8.0-oreo

我正在尝试在我的移动应用程序中运行此代码,但该代码未运行,也未给出错误。我也尝试打印日志,但是logcat中没有显示任何内容。此问题仅在Oreo中发生,并且在所有其他Android版本中运行良好,在应用程序处于后台运行时也运行良好。

public class MainActivity extends AppCompatActivity {

   AlarmManager am;
    TimeAlarm timeAlarm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        setOneTimeAlarm();
    }

    public void setOneTimeAlarm() {
        Intent intent = new Intent(this, TimeAlarm.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), pendingIntent);
    }

    public void setRepeatingAlarm() {
        Intent intent = new Intent(this, TimeAlarm.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), (5 * 1000), pendingIntent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("ondestroy","ondestroy");

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("onstart","onstart");

        IntentFilter intentFilter=new IntentFilter("my.custom.action.tag.fordemo");
        intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
        registerReceiver(timeAlarm,intentFilter);
    }
}

广播接收器的代码,当我从后台删除该应用程序后,它停止工作。

public class TimeAlarm extends BroadcastReceiver {

    NotificationManager nm;
    String channelId = "channel-01";
    String channelName = "Channel Name";
    int importance = NotificationManager.IMPORTANCE_HIGH;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onReceive(Context context, Intent intent) {
    nm = (NotificationManager) 
    context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel mChannel = new NotificationChannel(
                        channelId, channelName, importance);
                nm.createNotificationChannel(mChannel);
            } else {

            }
            CharSequence from = "Nithin";
            CharSequence message = "Crazy About Android...";
            PendingIntent contentIntent = 
            PendingIntent.getActivity(context, 0, new Intent(), 0);
            //Notification notif = new Notification(R.drawable.logo, 
           "Crazy About Android...", System.currentTimeMillis());
            NotificationCompat.Builder notification = new 
            NotificationCompat.Builder(context.getApplicationContext());
            // notification.setContentIntent(pintent);
       notification.setTicker(from).setSubText(from).setSmallIcon(R.drawable.logo);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notification.setChannelId(channelId);
            }

            Notification notification1 = notification.build();
            notif.setLatestEventInfo(context, from, message,contentIntent);
            nm.notify(1, notification1);
        }
    }        

3 个答案:

答案 0 :(得分:0)

在任何版本的OS的后台运行时,它都不应该工作。您正在onStart中注册接收器,然后在onStop中取消注册。这意味着在处理onStop之后,OS的接收器不存在。如果您发现它可以在其他版本的OS上运行,则实际上是一个错误。

答案 1 :(得分:0)

当然,在您的应用程序关闭时,您的broadcast将被杀死。如果您没有在unregisterReceiver / onStop中使用onDestroyed,则会遇到泄漏问题。为了避免它, 您应该实现一个Service(并在服务中调用registerReceiver(timeAlarm,intentFilter);),即使您的应用程序关闭,它也会在后台运行。

P / s:记得startForeground,以防止服务被Android 8.0或更高版本的系统杀死。

答案 2 :(得分:0)

您必须在清单中添加警报权限

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

还要注册您的广播

    <receiver
        android:name=".broadcast.ServiceBroadcast"
        android:enabled="true"
        android:exported="true"
        android:label="RestartServices">
        <intent-filter>
        <action android:name="restart_services"/>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>