gpt4 book ai didi

android - 在应用程序关闭后启动带有警报的服务

转载 作者:行者123 更新时间:2023-11-29 02:29:18 25 4
gpt4 key购买 nike

我正在制作一个简单的应用程序,其中包含一项每天多次弹出通知的服务。我正在尝试用闹钟来做这个。如果我不关闭应用程序,一切都会完美无缺。但是,如果我在接收器被触发时关闭应用程序,我会收到“应用程序已停止工作”或其他消息。

我在想是不是因为没有应用程序的 Activity 或其他什么东西,服务就无法工作?有什么见解吗?

这是我的代码:

接收者:

public class AlarmReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;
public static final String ACTION = "com.ddimitrovd.hap4eapp4e.alarm";

// Triggered by the Alarm periodically (starts the service to run task)
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MainIntentService.class);
i.putExtra("foo", "bar");
context.startService(i);
}
}

这是服务:

public class MainIntentService extends IntentService {

int noteID = 1;
String chanelID;

public MainIntentService() {
super("MainIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
chanelID = getString(R.string.channel_ID);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();

// Do the task here
createNotificationChannel();
popNotif();
}

public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
}

private void popNotif() {
// Create an explicit intent for an Activity in your app
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, chanelID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

notificationManager.notify(noteID, mBuilder.build());
}

private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(chanelID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}

我确定警报会触发接收器。

谢谢!

最佳答案

我想我找到了问题所在。我估计我的 IntentService 不能执行复杂,因为 BroadcastReceiver 进程在它可以执行之前就被杀死了。更多信息 here .我所做的只是在接收器中弹出我的通知,但我想更好的解决方案是启动另一个线程或将其异步。

关于android - 在应用程序关闭后启动带有警报的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50556471/

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