发布于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黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!