android AlarmManager闹钟设置多次只响应一次和响应多次以及传参的实现

在开发中,遇到设置闹钟但是遇到设置多次结果只响应一次,查阅好多资料终于解决了,现在把解决的方法分享一下,其次就是如果传参的问题,也一并贴出来供大家参考
代码如下:

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class AlamActivity extends AppCompatActivity implements View.OnClickListener{
    private Button only_btn;
    private Button repeat_btn;
    private AlarmManager mAlarmManager;
    public static int currentIntent=0;
    public static int alarmCount=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init(){
        only_btn =(Button)findViewById(R.id.btn_open);
        repeat_btn=(Button)findViewById(R.id.btn_close);
        only_btn.setOnClickListener(this);
        repeat_btn.setOnClickListener(this);
        only_btn.setText("Only");
        repeat_btn.setText("Repeat");
        mAlarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_open:
                Log.e("MainActivity","only");
                onlyShow();
                break;
            case R.id.btn_close:
                Log.e("MainActivity","repeat");
                repeatShow();
                break;
        }
    }
  	//只响应一次
    private void onlyShow(){
        long date1 = System.currentTimeMillis();
        Log.e("MainActivity","only闹钟开启时间为:"+date1);
        Intent intent =new Intent(this, AlarmBroadCast.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, date1+30000, pi);
    }
 	//只响应多次
    private void repeatShow(){
        long date1 = System.currentTimeMillis();
        Log.e("MainActivity","repeat闹钟开启时间为:"+date1);
        Intent intent =new Intent(this, AlarmBroadCast.class);
        intent.putExtra("date",date1+"");
        PendingIntent pi = PendingIntent.getBroadcast(this, alarmCount++, intent,currentIntent++ + PendingIntent.FLAG_UPDATE_CURRENT);//第四个参数区别每次注册的闹钟 第二个参数区别每次传递的参数
        mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, date1+30000, pi);
    }
}

广播类的代码:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AlarmBroadCast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String date = simpleDateFormat.format(new Date());
        String value = intent.getStringExtra("date");
        Log.e("MainActivity","闹钟时间为:"+date+"----参数为:"+value);
        Toast.makeText(context,date,Toast.LENGTH_SHORT).show();
    }
}

你可能感兴趣的:(android)