如何使用2 ToggleButton用于不同的2个不同的警报

时间:2016-10-31 13:17:42

标签: java android

我正在使用2个toggleButtons来执行不同的警报,如代码所示,当我激活这些按钮中的任何一个时,两个警报同时工作,那么我怎么能解决这个问题,因为我将使用2个以上

公共类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());

}

}

1 个答案:

答案 0 :(得分:0)

答案 是使用共享首选项保存切换按钮的状态然后设置警报或取消它取决于切换按钮的状态