如何将意图从BroadcastReciver传递到Activity

时间:2019-05-08 04:31:25

标签: broadcastreceiver alarmmanager

我编写了与“警报管理器”相关的代码,并且每天晚上12点运行, 并将意图从活动发送到BroadcastReciver类。直到这里好

如何将意图从BroadcastReciver发送到活动?我写了代码,但出错了。

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

    dateShow();
    prefss();
     //setRecurringAlarm(this);
    sss(this);
    Bundle z=getIntent().getExtras();
    if(z != null)
    {
        int a=z.getInt("kkk");
        Toast.makeText(MainActivity.this, "1 "+ a, Toast.LENGTH_SHORT).show();
        Log.i("kmmm1"," back" + a);
    }
}
 private void sss(Context context)
 {
     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
     Calendar calendar = Calendar.getInstance();
     calendar.set(Calendar.HOUR, 10);
     calendar.set(Calendar.MINUTE, 35);
    // calendar.set(Calendar.SECOND, 0);
     calendar.set(Calendar.AM_PM, Calendar.PM);
     Intent myIntent = new Intent(getApplicationContext(), BroadConnect.class);
    myIntent.putExtra("day1",Day_day);
     myIntent.putExtra("baghie1",_monde_pool);
    myIntent.putExtra("hazine1",Day_hazine);
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);
     long time1=calendar.getTimeInMillis();
     if(time1<System.currentTimeMillis())
     {
         time1 +=AlarmManager.INTERVAL_DAY;
     }
    /* alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time1,
             AlarmManager.INTERVAL_DAY, pendingIntent);*/
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time1,
             AlarmManager.INTERVAL_DAY, pendingIntent);
 }



 public class BroadConnect extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

        Toast.makeText(context,"okkkkk" ,Toast.LENGTH_LONG).show();
        Bundle z=intent.getExtras();
              int a=z.getInt("day1");
               int b=z.getInt("baghie1");
              int c=z.getInt("hazine1");
              Log.i("kmmm"," intent day1 " + a);
              Log.i("kmmm"," intent baghie1" + b);
              Log.i("kmmm"," intent hazine1" + c);

              Intent intent1=new Intent(context,MainActivity.class);
              intent1.putExtra("kkk","kk111");
              context.startActivity(intent1);

0 个答案:

没有答案