gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:37:46 25 4
gpt4 key购买 nike

我试图在我的移动应用程序中运行这段代码,但它没有运行,也没有给出任何错误。我也试过打印日志,但它在 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 之后,操作系统的接收器不存在。如果您发现它可以在其他版本的操作系统上运行,那实际上是一个错误。

关于java - 从后台删除应用程序时广播接收器不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53263566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com