2013-02-19 62 views
0

我已經創建了一個程序,它會根據日期選擇器手動創建不同的日期集。 的代碼工作properly.but如果重新啓動它丟失數據和報警不工作我怎樣才能克服重新啓動後報警沒有調用

我使用的代碼

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

    OnClickListener setClickListener = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      /** This intent invokes the activity DemoActivity, which in turn opens the AlertDialog window */ 
      Intent i = new Intent("in.com.example.demoactivity"); 

      /** Creating a Pending Intent */ 
      PendingIntent operation = PendingIntent.getActivity(getBaseContext(), count++, i, Intent.FLAG_ACTIVITY_NEW_TASK); 

      /** Getting a reference to the System Service ALARM_SERVICE */ 
      AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE); 

      /** Getting a reference to DatePicker object available in the MainActivity */ 
      DatePicker dpDate = (DatePicker) findViewById(R.id.dp_date); 

      /** Getting a reference to TimePicker object available in the MainActivity */ 
      TimePicker tpTime = (TimePicker) findViewById(R.id.tp_time); 

      int year = dpDate.getYear(); 
      int month = dpDate.getMonth(); 
      int day = dpDate.getDayOfMonth(); 
      int hour = tpTime.getCurrentHour(); 
      int minute = tpTime.getCurrentMinute(); 


      GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute); 

      long alarm_time = calendar.getTimeInMillis(); 

      /** Setting an alarm, which invokes the operation at alart_time */ 
      alarmManager.set(AlarmManager.RTC_WAKEUP , alarm_time , operation); 

      /** Alert is set successfully */ 
      Toast.makeText(getBaseContext(), "Alarm is set successfully",Toast.LENGTH_SHORT).show(); 

     } 
    };  

    OnClickListener quitClickListener = new OnClickListener() {   
     @Override 
     public void onClick(View v) { 
      finish(); 
     } 
    }; 

    Button btnSetAlarm = (Button) findViewById(R.id.btn_set_alarm); 
    btnSetAlarm.setOnClickListener(setClickListener); 

    Button btnQuitAlarm = (Button) findViewById(R.id.btn_quit_alarm); 
    btnQuitAlarm.setOnClickListener(quitClickListener); 

} 

從這一個活動片段

public class DemoActivity extends FragmentActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState); 

    /** Creating an Alert Dialog Window */ 
    AlertDemo alert = new AlertDemo(); 

    /** Opening the Alert Dialog Window */ 
    alert.show(getSupportFragmentManager(), "AlertDemo");  
} 

}

從這裏到創建alertbox活動

公共類AlertDemo擴展DialogFragment {

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    /** Turn Screen On and Unlock the keypad when this alert dialog is displayed */ 
    getActivity().getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON | LayoutParams.FLAG_DISMISS_KEYGUARD); 


    /** Creating a alert dialog builder */ 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

    /** Setting title for the alert dialog */ 
    builder.setTitle("Alarm"); 

    /** Setting the content for the alert dialog */ 
    builder.setMessage("An Alarm by AlarmManager"); 

    /** Defining an OK button event listener */ 
    builder.setPositiveButton("OK", new OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      /** Exit application on click OK */ 
      getActivity().finish(); 
     }      
    }); 

    /** Creating the alert dialog window */ 
    return builder.create(); 
} 

/** The application should be exit, if the user presses the back button */ 
@Override 
public void onDestroy() {  
    super.onDestroy(); 
    getActivity().finish(); 
} 

我想,即使我重新啓動設備警報點進行invoken,有人請幫我整理出來

回答

1

你必須使用中,你必須要BroadcastReceiver請檢查Intent.ACTION_BOOT_COMPLETED並重置Receiver中的警報操作。例如:

public class MyBootReceiver extends BroadcastReceiver{ 

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

     if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) { 
      //reset your alarm here 
     } 
    } 

} 

更新時間:

使用共享偏好您存儲數據,也可以使用數據庫了。我做了同樣的使用共享偏好,檢查下面的代碼:

public class MyBootReceiver extends BroadcastReceiver{ 

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

     if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) { 
      SharedPreferences mPreferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE); 
      String datetime = mPreferences.getString("date", null); 
      if(!TextUtils.isEmpty(datetime)) { 
       Utility.setNotification(context);//set your alarm here. 

      } 
     } 
    } 

} 
+0

我失去了所有的數據之前設置的,我不想這樣 – ammukuttylive 2013-02-19 11:10:09

+0

檢查我更新了我的答案。 – RobinHood 2013-02-19 11:34:14

+0

讓我試試數據庫希望它能工作。 – ammukuttylive 2013-02-19 11:40:24