即使應用程序關閉,我也想使鬧鐘運行, 當應用程序未關閉時,一切正常, 但當觸發時間並關閉應用程序時,時間到了應用程序崩潰的時候。 我嘗試了一些解決方案,但他們沒有幫助。當應用程序關閉時廣播接收器
這裏是我的代碼:
public class MainActivity extends Activity {
//used for register alarm manager
PendingIntent pendingIntent;
//used to store running alarmmanager instance
AlarmManager alarmManager;
//Callback function for Alarmmanager event
BroadcastReceiver mReceiver;
TimePicker tp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Register AlarmManager Broadcast receive.
RegisterAlarmBroadcast();
tp = (TimePicker)findViewById(R.id.timePicker1);
tp.setIs24HourView(true);
Calendar cal=Calendar.getInstance();
tp.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));
tp.setCurrentMinute(cal.get(Calendar.MINUTE));
}
public void onClickSetAlarm(View v)
{
//Get the current time and set alarm after 10 seconds from current time
// so here we get
Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour());
time.set(Calendar.MINUTE, tp.getCurrentMinute());
time.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
tp.setEnabled(false);
// alarmManager.set(AlarmManager.RTC_WAKEUP,
// System.currentTimeMillis() + Integer.parseInt(time.getText().toString()) * 1000 , pendingIntent);
}
private void RegisterAlarmBroadcast()
{
// Log.i("Alarm Example:RegisterAlarmBroadcast()", "Going to register Intent.RegisterAlramBroadcast");
//This is the call back function(BroadcastReceiver) which will be call when your
//alarm time will reached.
mReceiver = new BroadcastReceiver()
{
private static final String TAG = "Alarm Example Receiver";
@Override
public void onReceive(Context context, Intent intent)
{
//Log.i(TAG,"BroadcastReceiver::OnReceive() >>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
Toast.makeText(context, "Congrats!. Your Alarm time has been reached", Toast.LENGTH_LONG).show();
tp.setEnabled(true);
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent("MY_ALARM_NOTIFICATION").setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0);
// this is it, we'll build the notification!
// in the addAction method, if you don't want any icon, just set the first param to 0
Notification mNotification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Post!")
.setContentText("Here's an awesome update for you!")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setSound(soundUri)
// .addAction(R.drawable.ninja, "View", pIntent)
.addAction(0, "Remind", pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// If you want to hide the notification after it was selected, do the code below
// myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, mNotification);
}
};
// register the alarm broadcast here
registerReceiver(mReceiver, new IntentFilter("MY_ALARM_NOTIFICATION"));
pendingIntent = PendingIntent.getBroadcast(this, 123456789, new Intent("MY_ALARM_NOTIFICATION").setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),0);
alarmManager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
private void UnregisterAlarmBroadcast()
{
alarmManager.cancel(pendingIntent);
getBaseContext().unregisterReceiver(mReceiver);
}
@Override
protected void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
}
任何形式的幫助,將不勝感激。
這裏是我的manifest.xml:
<receiver android:name=".MainActivity" >
<intent-filter>
<action android:name="MY_ALARM_NOTIFICATION" />
</intent-filter>
</receiver>
它究竟如何崩潰?請發佈logcat轉儲。 – 2014-10-20 20:22:59