我希望在重启设备后恢复/重启警报。经过对Google的大量研究后发现,如果不将警报保存到数据库中,它就无法运行。我有两个问题:
2.我应该如何在重启时从数据库中读取警报,因为广播接收者没有收到BOOT_COMPLETED意图。
1.如何将警报保存到数据库中(我怎么知道创建了数据库并插入了值?)
EDIT:
我已经拥有必要的权限和接收者以及此代码: 告诉我这段代码要改变什么? MainActivity.java
onClick:
int intHrs = SetResetIntervalActivity.intHours;
int intMins = SetResetIntervalActivity.intMinutes;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, intHrs);
calendar.set(Calendar.MINUTE, intMins);
calendar.set(Calendar.SECOND, 0);
Intent myIntent = new Intent(
"com.sang.mobiledata.IntentAction.RECEIVE_RESETCONN_UPDATE");
myIntent.putExtra("FLAG_KEY", false);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
long interval = intHrs * 3600000 + intMins * 60000;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), interval, pi);
long mins = interval / 60000;
Toast.makeText(
MainActivity.this,
"Data Connection will be reset every " + mins
+ " minute(s).", Toast.LENGTH_SHORT).show();
广播接收器:
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(
"android.intent.action.BOOT_COMPLETED")) {
//control never enters here
boolean blnVar=true;
blnVar=false;
....
清单:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name="com.sang.mobiledata.ResetBroadcastReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.sang.mobiledata.IntentAction.RECEIVE_RESETCONN_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="com.sang.mobiledata.IntentAction.BOOT_COMPLETED" />
</intent-filter>
</receiver>
答案 0 :(得分:0)
您可以按照以下方式重新设置“设备启动时的警报”,而不是将警报保存到数据库,
// BroadcastReceiver
public class StartApplication extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
context.startService( new Intent ( context, AlarmService.class ) );
}
}
//警报类
public class AlarmService extends Service
{
@Override
public int onStartCommand ( Intent intent,int flags, int startId )
{
Intent alarmIntent = new Intent ( this,RepeaterService.class );
PendingIntent pendingIntent = PendingIntent.getService( this, 0, alarmIntent, 0 );
AlarmManager alarm = ( AlarmManager )getSystemService( Context.ALARM_SERVICE );
alarm.setRepeating( AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60*1000, pendingIntent );
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent)
{
// TODO Auto-generated method stub
return null;
}
}
您需要在AndroidManifest.xml中添加以下权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
以下方式您可以在AndroidManifest.xml中声明您的BroadcastReceiver,
<receiver android:name="com.example.stackoverflow.StartApplication" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
在特定时间设置警报时,在sqlite数据库中保存警报的小时,分钟,秒和警报的值。重新启动后,从BroadCast启动服务,并在该服务的onStart()方法中检索警报值并再次设置警报使用未决意图。
欢迎您询问是否有任何疑问。