1

앱이 닫힌 경우에도 실행중인 알람 시계를 만들고 싶습니다. 앱이 닫히지 않았을 때 모든 것이 잘 작동하지만 시간을 트리거하고 앱을 닫을 때 순간이 앱 충돌로 이어집니다. 몇 가지 해결책을 시도했지만 도움이되지 않았습니다. 도움의 모든 종류를 감상 할 수있다앱이 닫힐 때 브로드 캐스트 수신자

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> 
+0

정확히 어떻게 충돌합니까? logcat 덤프를 게시하십시오. <수신기 안드로이드 : 이름 = "MainActivity가."> <의도 필터> 도와 –

답변

1

Activity을 매니페스트에 <receiver>으로 정의했습니다. 그건 안돼.

여기에 몇 가지 문제가 있습니다.

  1. BroadcastReceiver<receiver> 태그가있는 매니페스트에서 선언해야합니다. 즉, 사용중인 수업은 BroadcastReceiver으로 공개되어야합니다.
  2. 코드에서 귀하의 BroadcastReceiver을 익명으로 선언하셨습니다. 일반적으로이 작업은 가능하지만 앱 외부의 구성 요소에서 BroadcastReceiver을 호출해야하는 경우에는 작동하지 않습니다. AlarmMnager은 앱 외부에서 실행됩니다 (실제로 앱이 실행되지 않을 때 알람이 울리기도합니다). 따라서 BroadcastReceiverBroadcastReceiver으로 확장되는 공용 클래스로 정의되어야합니다.
  3. PendingIntent.getActivity()으로 전화하십시오. BroadcastReceiver을 시작하려면 PendingIntent.getBroadcast()으로 전화해야합니다.
  4. onReceive() 메서드에서 앱이 실행되지 않는 동안 알람이 트리거 된 경우 null이 될 멤버 변수를 사용하고 있습니다. 앱이 실행 중이 아니더라도 AlarmManager이 알람을 울릴 수 있으므로 onReceive()이 호출 될 때 모든 멤버 변수이 설정되었다고 기대할 수 없습니다.

이 작업을 수행하는 방법에 대한 몇 가지 예를 살펴 보시기 바랍니다. 사용할 수있는 톤이 많습니다.

2

하지만 응용 프로그램이 사망하고 Receiver 당신 되었기 때문에 등록 된 사람도 죽었으므로 에 NullPointerException 어딘가에 있습니다. 의도적으로 Activity이 멀리 떨어져 있음을 알 때 (예 : onStop()) 알람을 등록 취소하거나 제거해야합니다. 앱이 표시되지 않는 및/또는이 닫혀 후 때 알람이 작동 할 경우

  1. 당신은 당신의 AndroidManifest.xml에 수신기를 등록해야합니다. 앱 내에서 프로그래밍 방식으로 등록 된 수신기는 앱을 탐색 한 후 살아서는 안되며 사용자가 앱을 종료하면 앱과 함께 삭제됩니다.

  2. 수신기에 BOOT_COMPLETED에 등록 할 수도 있습니다. 기기가 다시 시작될 때 모든 알람은 이 (가)으로 손실되므로 기기를 재부팅 할 때마다 알람을 다시 설정해야합니다 (SharedPreferences 플래그로 설정해야하는지 확인).

+0

감사의 여기 내 매니페스트의 <액션 안드로이드 : 이름 = "MY_ALARM_NOTIFICATION"/> zb22

관련 문제