gpt4 book ai didi

android - 应用多次收到 BOOT_COMPLETED 并再次重新创建警报

转载 作者:行者123 更新时间:2023-11-30 01:11:59 25 4
gpt4 key购买 nike

我做了一个应用程序作为教程来学习如何在手机重启后重新启动已删除的闹钟。重新启动后,BroadcastReceiver 收到 BOOT_COMPLETED 操作并启动服务,该服务将重新启动所有警报。警报在被调用时创建通知。第一次测试后,我在预期的时间收到了通知,然后忘记了这个教程应用程序,也没有关闭手机。

但是,我的屏幕上仍然收到通知,就好像应用程序再次重启了闹钟一样。白天我收到了两次,都不清楚为什么。唯一可能启动重新启动警报的是 BOOT_COMPLETED 操作。这里出了什么问题?

list :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.marat.recyclerviewtutorial">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity

// list of activities

<service android:name=".BootService"/>

<receiver android:name=".AlarmBroadcastReceiver"/>

<receiver android:name=".RestartAlarmsReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

</application>

</manifest>

RestartAlarmsReceiver.java

public class RestartAlarmsReceiver extends BroadcastReceiver {

private static final String TAG = "myTag";

@Override
public void onReceive(Context context, Intent intent) {

if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {

Intent i = new Intent(context, BootService.class);
ComponentName service = context.startService(i);

if (null == service) {
// something really wrong here
Log.e(TAG, "Could not start service ");
}
else {
Log.e(TAG, "Successfully started service ");
}

} else {
Log.e(TAG, "Received unexpected intent " + intent.toString());
}
}
}

BootService.java

public class BootService extends Service {

private static final String TAG = "myTag";

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Log.d(TAG, "onStartCommand");

Database db = new Database(this);
db.open();
ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
data = db.getData();
db.close();

Log.d(TAG, "db is openned");

for (int i=0; i<data.size(); i++) {

Intent notif = new Intent(this, AlarmBroadcastReceiver.class);
notif.putExtra("Text", data.get(i).get(2));
notif.putExtra("Position", data.get(i).get(0));

int sec = Integer.parseInt(data.get(i).get(3));

Log.d(TAG, "intent is openned");

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, Integer.parseInt(data.get(i).get(0)), notif, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (sec * 1000), pendingIntent);

Log.d(TAG, "Alarm is restarted");
}

Log.d(TAG, "before returning service");

return Service.START_STICKY;
}
}

这已经是很大一部分代码了。但如果您需要我的应用程序的其他部分,我会包含它们。

提前致谢!

最佳答案

我认为这是因为 START_STICKY。我认为白天系统会关闭您正在运行的服务以释放资源,并且 START_STICKY 会强制它重新启动。并在重新启动时再次触发 onStartCommand()

决定可能会使用 IntentService 代替。我看不到在这里使用服务的必要性。

关于android - 应用多次收到 BOOT_COMPLETED 并再次重新创建警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38321945/

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