앱이 닫힌 경우에도 실행중인 알람 시계를 만들고 싶습니다. 앱이 닫히지 않았을 때 모든 것이 잘 작동하지만 시간을 트리거하고 앱을 닫을 때 순간이 앱 충돌로 이어집니다. 몇 가지 해결책을 시도했지만 도움이되지 않았습니다. 도움의 모든 종류를 감상 할 수있다앱이 닫힐 때 브로드 캐스트 수신자
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입니다 : 심지어 나는AlarmManager
알람을 제공하려고 추측 것이다 스택 추적을 보지 않고
<receiver android:name=".MainActivity" >
<intent-filter>
<action android:name="MY_ALARM_NOTIFICATION" />
</intent-filter>
</receiver>
정확히 어떻게 충돌합니까? logcat 덤프를 게시하십시오. <수신기 안드로이드 : 이름 = "MainActivity가."> <의도 필터> 도와 –