2012-05-09 68 views
1

我遇到了麻烦,开机后无法再正确设置闹钟。BroadcastEvent,闹钟功能android

public void onReceive(Context context, Intent intent) { 

    if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){ 
     if(fm.getAlarmBool()){ 
      time = fm.getAlarmTimeLong(); 
      reminder.startAlarm(time); 
     } 
    } 

这是我想在启动后运行的方法。我已经在Android清单中添加了权限,但我无法使其正常工作。怎么了?

+0

请从提醒中添加一些附加代码cos,fm不可能回答。 – Mahesh

+0

提醒类使用日历来设置一个固定的时间,例如,如果我设置时间15:20,那么它在那个时候设置一个闹钟事件,fm只是将一个字符串写入一个文件,然后从文件中提取信息。在这种情况下,它会写入事件发生的时间。在我看来,它不需要解决这个问题。 – Anticipating

回答

0

你应该做这样

public class BootReceiver extends BroadcastReceiver { 

Context context; 
@Override 
public void onReceive(Context context, Intent intent) {   
    context.startService(new Intent(context, NotificationService.class));  
} 

}

清单中

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

所以在这里为您服务收集所有的提醒和重新计划。 为此,您可以将其存储在共享的pref或数据库中以进行永久存储

+0

我需要在NotifactionService上写什么类?我想在设备启动时运行的类?你在我的IF声明中看到任何错误吗?当我将你的代码放在onReceive方法下并重新启动我的手机时,警报立即生效。我是否需要在某种声明中说明这一点? – Anticipating