2016-10-31 4 views
0

私はこれらのボタンのいずれかをアクティブにすると、コードに示されているように異なるアラームに2つのトグルボタンを使用しています。 iが2以上異なる2つの異なるアラームに対して2 ToggleButtonを使用する方法

パブリッククラスMainActivityを使用するようAppCompatActivityを拡張{

ToggleButton toggleButton , toggleButton1 ; 
    AlarmManager alarmManager; 
    PendingIntent pendingIntent; 

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

     toggleButton = (ToggleButton)findViewById(R.id.togelbutton); 
     toggleButton1 = (ToggleButton)findViewById(R.id.togelbutton1); 

     toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (isChecked){ 

        Intent intent = new Intent(getApplicationContext(),AlarmReceiver.class); 

        pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT); 

        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), 1000*10,pendingIntent); 
       } 
       else{ 
        alarmManager.cancel(pendingIntent); 
        Toast.makeText(getApplicationContext(),"noti ended",Toast.LENGTH_LONG).show(); 

       } 
      } 
     }); 

     toggleButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (isChecked){ 

        Intent intent = new Intent(getApplicationContext(),AlarmReceiver.class); 

        pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),2,intent,PendingIntent.FLAG_UPDATE_CURRENT); 

        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), 1000*10,pendingIntent); 
       } 
       else{ 
        alarmManager.cancel(pendingIntent); 
        Toast.makeText(getApplicationContext(),"noti ended",Toast.LENGTH_LONG).show(); 

       } 
      } 
     }); 

    } 
} 



public class AlarmReceiver extends BroadcastReceiver { 

NotificationCompat.Builder notificationCompat; 
private static final int uniqueID = 24314; 


@Override 
public void onReceive(Context context, Intent intent) { 

    Toast.makeText(context,"welcome here",Toast.LENGTH_LONG).show(); 

    alarm1(context); 
    alarm2(context); 

} 

public void alarm1 (Context context){ 

    notificationCompat = new NotificationCompat.Builder(context); 
    notificationCompat.setAutoCancel(true); 
    notificationCompat .setTicker("Notification1"); 
    notificationCompat .setContentTitle("Notification 1"); 
    notificationCompat .setContentText("Good Job"); 
    notificationCompat .setSmallIcon(R.mipmap.ic_launcher); 
    notificationCompat.setDefaults(Notification.DEFAULT_SOUND); 
    notificationCompat.setVibrate(new long[] {100,2000,100,2000}); 
    notificationCompat.setLights(Color.BLUE,400,400); 


    Intent intent1 = new Intent(context,MainActivity.class); 
    intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent1,PendingIntent.FLAG_UPDATE_CURRENT); 

    notificationCompat.setContentIntent(pendingIntent); 
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(uniqueID,notificationCompat.build()); 

} 

public void alarm2 (Context context){ 

    notificationCompat = new NotificationCompat.Builder(context); 
    notificationCompat.setAutoCancel(true); 
    notificationCompat .setTicker("Notification2"); 
    notificationCompat .setContentTitle("Notification 2"); 
    notificationCompat .setContentText("Good Job"); 
    notificationCompat .setSmallIcon(R.mipmap.ic_launcher); 
    notificationCompat.setDefaults(Notification.DEFAULT_SOUND); 
    notificationCompat.setVibrate(new long[] {100,2000,100,2000}); 
    notificationCompat.setLights(Color.BLUE,400,400); 


    Intent intent1 = new Intent(context,MainActivity.class); 
    intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(context,2,intent1,PendingIntent.FLAG_UPDATE_CURRENT); 

    notificationCompat.setContentIntent(pendingIntent); 
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(uniqueID,notificationCompat.build()); 

} 

}

+0

こんにちは。私の目を引く最初のことは、同じ保留中の意図を使用して、それを上書きすることです、私はそれをcheckChangedメソッドでプライベートフィールドにすることをお勧めします、または、最初のステップとして2人のメンバーを作る。 – Lev

答えて

0

答え は、トグルボタンの状態は、アラームを設定し保存またはキャンセルするために共有プリファレンスを使用することですそれはdeトグルボタンの状態を保つ

関連する問題