23

在我的应用程序中,我想每天在特定时间使用AlarmManager. 在android文档中我发现了这个:

已注册的警报在设备处于睡眠状态时会保留 [...],但如果关闭并重新启动,则会被清除。

这就是问题所在。即使用户重新启动手机,我也想运行代码。如果用户重新启动手机,他目前必须重新启动我的应用程序才能再次启动警报。我怎样才能防止这种情况?我应该使用更好的机制吗?

4

3 回答 3

36

使用以下代码创建引导接收器:

public class BootBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context pContext, Intent intent) {
        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
            // Do your work related to alarm manager
        }
    }
}

在您的清单中,注册此广播接收器:

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

并且不要忘记在 AndroidManifest.xml 添加权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
于 2013-07-16T10:20:40.217 回答
7

使用你可以在设备启动时使用广播接收器创建服务吗

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

允许:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
于 2013-07-16T10:16:51.340 回答
3

您需要像这样在清单中添加一个引导接收器

<application ...  >

    <receiver android:name=".OnBootReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <uses-permission android:name="android.permission.WAKE_LOCK" />
        </intent-filter>
    </receiver>
</application>

然后像这样创建引导接收器类...

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

public class OnBootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context ctxt, Intent intent) {
AlarmHelper.setAlarm(ctxt);
}
}

我的闹钟助手类是一个简单的开始闹钟,像这样......

public class AlarmHelper {

public static void testAlarm(Context context) {
Calendar when = Calendar.getInstance();
when.add(Calendar.SECOND, 10);
setAlarm(context, when);    
}

public static void setAlarm(Context context) {
Calendar when = Calendar.getInstance();
when.add(Calendar.DAY_OF_YEAR, 1);
when.set(Calendar.HOUR_OF_DAY, 0);
when.set(Calendar.MINUTE, 0);
when.set(Calendar.SECOND, 0);
setAlarm(context, when);
}

    @SuppressLint("SimpleDateFormat")
private static void setAlarm(Context context, Calendar when) {

SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());

Boolean showNotifications = prefs.getBoolean("PREF_SHOW_NOTIFICATIONS",
false);

if (showNotifications) {    
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);

am.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), AlarmManager.INTERVAL_DAY, getPendingIntent(context.getApplicationContext()));

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Log.i(TAG, "Alarm set " + sdf.format(when.getTime()));
}
}
于 2013-07-16T10:20:42.937 回答