2016-12-27 84 views
0

我想在每天下午12點設置提醒,當用戶在交換機上時。在isChecked()我有setText爲「在下午12:00提醒」。我想在那裏設置鬧鐘或提醒。代碼如下:如何在Android中切換isChecked()時設置提醒?

private RadioButton radioButtonPlayback, radioButtonTranslate; 
    private TextView switchStatus; 
    private Switch mySwitch; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 


     switchStatus = (TextView) findViewById(R.id.switchStatus); 
     mySwitch = (Switch) findViewById(R.id.mySwitch); 

     //set the switch to ON 
     mySwitch.setChecked(true); 
     //attach a listener to check for changes in state 
     mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

       if (isChecked) { 
        switchStatus.setText("Remind at: 12:00 PM"); 

       } else { 
        switchStatus.setText("No reminders set"); 
       } 

      } 
     }); 

     //check the current state before we display the screen 
     if (mySwitch.isChecked()) { 
      switchStatus.setText("Remind at: 12:00 PM"); 
     } else { 
      switchStatus.setText("No reminders set"); 
     } 
+0

您面臨的問題是什麼? –

+0

您是否在尋找設置提醒的代碼? –

+0

@ReadyAndroid我沒有面臨任何問題。我只想知道當狀態是isChecked()時我該如何設置一個提醒() –

回答

0

嘗試這種情況:

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 

    { 

     @Override 
     public void onCheckedChanged (CompoundButton buttonView,boolean isChecked){ 
     if (isChecked) { 
      Intent myIntent = new Intent(MainActivity.this, AlarmReceiver AlarmReceiver.class); 
      pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0); 
      AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      calendar.set(Calendar.HOUR_OF_DAY, 12); 
      calendar.set(Calendar.MINUTE, 59); 
      calendar.set(Calendar.SECOND, 0); 
      alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
     } 
    } 
    } 

    ); 

AlarmReceiverClass:

public class AlarmReceiver extends WakefulBroadcastReceiver { 

    @Override 
    public void onReceive(final Context context, Intent intent) { 
     //this will update the UI with message 
     AlarmActivity inst = AlarmActivity.instance(); 
     inst.setAlarmText("Alarm! Wake up! Wake up!"); 

     //this will sound the alarm tone 
     //this will sound the alarm once, if you wish to 
     //raise alarm in loop continuously then use MediaPlayer and setLooping(true) 
     Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
     if (alarmUri == null) { 
      alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     } 
     Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri); 
     ringtone.play(); 

     //this will send a notification message 
     ComponentName comp = new ComponentName(context.getPackageName(), 
       AlarmService.class.getName()); 
     startWakefulService(context, (intent.setComponent(comp))); 
     setResultCode(Activity.RESULT_OK); 
    } 
} 

AlarmService類別:

public class AlarmService extends IntentService { 
    private NotificationManager alarmNotificationManager; 

    public AlarmService() { 
     super("AlarmService"); 
    } 

    @Override 
    public void onHandleIntent(Intent intent) { 
     sendNotification("Wake Up! Wake Up!"); 
    } 

    private void sendNotification(String msg) { 
     Log.d("AlarmService", "Preparing to send notification...: " + msg); 
     alarmNotificationManager = (NotificationManager) this 
       .getSystemService(Context.NOTIFICATION_SERVICE); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, AlarmActivity.class), 0); 

     NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
       this).setContentTitle("Alarm").setSmallIcon(R.drawable.ic_launcher) 
       .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) 
       .setContentText(msg); 


     alamNotificationBuilder.setContentIntent(contentIntent); 
     alarmNotificationManager.notify(1, alamNotificationBuilder.build()); 
     Log.d("AlarmService", "Notification sent."); 
    } 
} 

在menifest:

<service 
      android:name=".AlarmService" 
      android:enabled="true" /> 
     <receiver android:name=".AlarmReceiver" /> 
+0

我需要在清單文件中添加任何東西嗎? –

+0

我不這麼認爲。你有沒有嘗試過的代碼?你是否在日誌中獲得任何權限錯誤? – Nidhi

+0

是的,我嘗試了代碼,但沒有收到任何警報通知。我應該在MyAlarmService.class文件中寫什麼? –