gpt4 book ai didi

java - 手机重启后闹钟管理器不起作用

转载 作者:行者123 更新时间:2023-12-02 13:03:53 24 4
gpt4 key购买 nike

当我设置闹钟,然后重新启动设备时,闹钟停止工作。我正确注册了 BroadcastReciver 并创建了 Boot BroadCastReciver,但设备重启后仍然无法使用。

我的代码:

第一次设置闹钟的MainActivty:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putString("language","1");
editor.commit();

setRecurringAlarm(this);

}

private void setRecurringAlarm(Context context) {

Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getTimeZone("GMT+5:00"));
updateTime.set(Calendar.HOUR_OF_DAY, 3);
updateTime.set(Calendar.MINUTE, 00);

Intent intent = new Intent(context, Tasks.class);
PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarms = (AlarmManager) this.getSystemService(
Context.ALARM_SERVICE);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, recurringDownload);
}
}

广播接收器任务:

public class Tasks extends BroadcastReceiver {

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

Intent i = new Intent();
i.setClassName(context, Main2Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}

启动广播接收器:

public class BootBroadcastReceiver extends BroadcastReceiver {

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

Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getTimeZone("GMT+5:00"));
updateTime.set(Calendar.HOUR_OF_DAY, 3);
updateTime.set(Calendar.MINUTE, 00);

Intent i = new Intent(context, Tasks.class);
PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
0, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarms = (AlarmManager) context.getSystemService(
Context.ALARM_SERVICE);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, recurringDownload);
}
}

AndroidManifest:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission
android:name="android.permission.MODIFY_PHONE_STATE"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
.
.
.

<receiver android:name=".BootBroadcastReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver android:name=".Tasks"
android:enabled="true">
</receiver>

</application>

最佳答案

您需要服务和广播接收器来在手机重新启动时唤醒您的应用程序。这里我有服务代码可能会对您有所帮助(它在我的应用程序中完美运行)。

服务

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;


public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new GPSTracker(ReunirseService.this);
return START_STICKY;
}
}

接收器将接收启动完成的 Intent 并启动您的服务。

    import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver {
RestartServiceReceiver alarm = new RestartServiceReceiver();
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
Intent i = new Intent(context, MyService .class);
context.startService(i);
}

}
}

并且需要添加RECEIVE_BOOT_COMPLETED权限

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

还需要添加过滤器

        <receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

关于java - 手机重启后闹钟管理器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44211576/

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