程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

当应用程序从后台删除时,广播接收器不工作?

发布于2021-08-03 08:37     阅读(767)     评论(0)     点赞(2)     收藏(4)


我正在尝试在我的移动应用程序中运行此代码,但它没有运行,也没有出现错误。我也试过打印日志,但它在 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);
        }
    }        

解决方案


它不应该在任何版本的操作系统的后台运行。您正在 onStart 中注册接收器,然后在 onStop 中取消注册。这意味着在处理 onStop 之后,OS 的接收器不存在。如果您看到它适用于其他版本的操作系统,那实际上是一个错误。



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/254446/cabef3ade41f9b927e61/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

2 0
收藏该文
已收藏

评论内容:(最多支持255个字符)