手机重启后报警未运行

时间:2014-08-21 13:00:56

标签: android alarmmanager

我正在开发一个可以设置闹钟的应用程序但是当我重新启动系统时,应用程序崩溃并且警报无法在启动后重置(警报不再处于活动状态)。请帮我解决这个问题。我正在使用 BroadcastReceiver 来实现此目的。

    public  void setAlarm(Calendar targetCal) {
    db();
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
      intent.putExtra("text", two);
       intent.putExtra("id", "" );
       PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1,  intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),pendingIntent);
    System.out.println("running setAlarm  ...");
   //       numBoth=numBoth+1;
  //        numCyn=numCyn+1;
 //     numEss=numEss+1;
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), 86400000, pendingIntent);

}    



public class AlarmReceiver extends BroadcastReceiver {

Intent intent;
String msg;
RingtoneManager ringtoneM;
//  private int[] SeqSwipeSound = new int[] { R.raw.swipe_1,
//          R.raw.right_color1, R.raw.wrong_color };
private int next=0;
SharedPreferences prefs;
String ringtone;
private SQLiteDatabase database;

private static final String DB_NAME = "essential";

private static final String TABLE_NAME = "tone";



private static final String TEXT = "name";

 @SuppressWarnings("static-access")
 @Override
 public void onReceive(final Context arg0, final Intent arg1) {
/* if(arg1.getAction().equals(arg1.ACTION_BOOT_COMPLETED)){


 }
 else {*/

// android.os.Debug.waitForDebugger();

 try {


      Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show();
      ExternalDbOpenHelper dbOpenHelperT = new ExternalDbOpenHelper(arg0, DB_NAME);
        database = dbOpenHelperT.openDataBase();
        database = dbOpenHelperT.getWritableDatabase();

     /* Intent i = new Intent(arg0,AlarmSettingsActivity.class);
      i.putExtra("alarm", true);

        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        arg0.startActivity(i);*/

      Bundle b = arg1.getExtras();

      long id = /*intent.getIntExtra("id",0);*/ 10;
      msg =  b.getString("text");;

      Intent notificationIntent = null;

      notificationIntent = new Intent(arg0, KarmicQuote.class);
      notificationIntent.putExtra("text", msg);
      notificationIntent.putExtra("id", "");
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);


      Notification noti = new Notification(R.drawable.app_icon_1, msg, System.currentTimeMillis());
      PendingIntent pi = PendingIntent.getActivity(arg0, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

      noti.setLatestEventInfo(arg0, "Essential Insights", msg, pi);
      // TODO check user preferences
      noti.defaults |= Notification.DEFAULT_VIBRATE;
      //noti.sound = Uri.parse(AlarmSettingsActivity.());
      //noti.defaults |= Notification.DEFAULT_SOUND;\

      noti.defaults |= Notification.DEFAULT_LIGHTS;
      noti.flags |= Notification.FLAG_AUTO_CANCEL;     


  //          AlarmSettingsActivity act= new AlarmSettingsActivity();
   //          act.db();
    //  AudioManager audioManager = (AudioManager)         arg0.getSystemService(Context.AUDIO_SERVICE);



      String name = null;
        try
        {
            Cursor c = null;
            c = database.rawQuery("select name from tone", null);
            c.moveToFirst();
            name = c.getString(c.getColumnIndex("name"));
            System.out.println("Get:"+name);

            c.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }



            Uri tone = Uri.parse(name);
            noti.sound = tone;
      NotificationManager nm = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
      nm.notify((int)id, noti);

} catch (Exception e) {
    // TODO: handle exception
}
 }

}

清单文件中的

   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
   <receiver
        android:name="com.essentialsguide.AlarmReciever"
        android:process=":remote" >
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />

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

0 个答案:

没有答案