Search code examples
androidbroadcastreceiveralarmmanagersleep

Android sleeptimer with broadcastreceiver


I am making an audioplayer app in Android. I'm trying to make sleeptimer, but it doesnt work. I want to make the app close automatically when it meets the set time. I have considered it over 5 days... Please help me

Here is my part of activity.

String items[] = {"타이머 없음", "1분", "3분", "5분", "10분", "15분", "20분",         "30분", "40분", "1시간", "2시간"};
 AlertDialog.Builder ab = new AlertDialog.Builder(this);
 ab.setTitle("SleepTimer");
 ab.setSingleChoiceItems(items, 0,
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            try {
                int broadcastUniqId = 0x1123; // uniq id 설정
                Intent intent = new Intent("action_alarm_notification");
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), broadcastUniqId, intent, 0);
                AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);


                alarmManager.cancel(pendingIntent);
                pendingIntent.cancel();

                int intervalTime = 0;
                switch (whichButton) {
                    case 0: 
                        intervalTime = 0;
                       break;
                    case 1:// 1분
                        intervalTime = 60000;
                       break;
                    case 2:// 3분
                        intervalTime = 180000;
                       break;
                    case 3:// 5분
                        intervalTime = 300000;
                       break;
                    case 4:// 10분
                        intervalTime = 600000;
                       break;
                    case 5:// 15분
                        intervalTime = 900000;
                       break;
                    case 6:// 20분
                        intervalTime = 1200000;
                       break;
                    case 7:// 30분
                        intervalTime = 1800000;
                       break;
                    case 8:// 40분
                        intervalTime = 2400000;
                       break;
                    case 9:// 1시간
                        intervalTime = 3600000;
                       break;
                    case 10:// 2시간
                        intervalTime = 7200000;
                       break;
                }


                if (intervalTime > 0) {
                    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), broadcastUniqId, intent, 0);
                    alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ intervalTime, pendingIntent);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            dialog.dismiss();
        }
    });
  ab.show();

This is BroadcastReceiver

 public class AlarmBroadcastReceiver extends BroadcastReceiver {
     @Override
      public void onReceive(Context context, Intent intent) {
          if (intent.getAction().equals("action_alarm_notification")) {
         System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());
    }
}

}

This is manifest.

  <receiver
      android:name=".AlarmBroadcastReceiver"
     android:enabled="true"
      android:exported="false">
    <intent-filter>
    <action android:name="action_alarm_notification"/>
   </intent-filter>
  </receiver>

Solution

  • You can close your application onReceive() Look the links below :

    this, or this