Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

android - Intent getting null in onReceive in MyAlarm class even though I sat putExtra while sending intent

MainActivity:

package com.jimmytrivedi.alarmdemo;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = MainActivity.class.getSimpleName();

    @BindView(R.id.timePicker)
    TimePicker timePicker;
    @BindView(R.id.buttonAlarm)
    Button buttonAlarm;
    @BindView(R.id.cancelAlarm)
    Button cancelAlarm;

    private AlarmManager alarmManager;
    private PendingIntent pendingIntent;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        cancelAlarm.setEnabled(false);
        selClickListener();
    }

    private void selClickListener() {
        buttonAlarm.setOnClickListener(this);
        cancelAlarm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.buttonAlarm:
                Calendar calendar = Calendar.getInstance();
                if (Build.VERSION.SDK_INT >= 23) {
                    calendar.set(
                            calendar.get(Calendar.YEAR),
                            calendar.get(Calendar.MONTH),
                            calendar.get(Calendar.DAY_OF_MONTH),
                            timePicker.getHour(), timePicker.getMinute(), 0);
                } else {
                    calendar.set(
                            calendar.get(Calendar.YEAR),
                            calendar.get(Calendar.MONTH),
                            calendar.get(Calendar.DAY_OF_MONTH),
                            timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
                }
                setAlarm(calendar.getTimeInMillis());
                showLog("getTimeInMillis: "+calendar.getTimeInMillis() );
                cancelAlarm.setEnabled(true);

                break;

            case R.id.cancelAlarm:
                cancelAlarm();
                break;
        }
    }

    private void setAlarm(long time) {
        //creating a new intent specifying the broadcast receiver
         Intent intent = new Intent(this, MyAlarm.class);
         intent.putExtra("REMINDER_ID", "1");

        //creating a pending intent using the intent
         pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        //setting the repeating alarm that will be fired every day
        alarmManager.setRepeating(AlarmManager.RTC, time, AlarmManager.INTERVAL_DAY, pendingIntent);
        Toast.makeText(this, "Alarm is set", Toast.LENGTH_SHORT).show();

    }

    private void cancelAlarm() {
        pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        alarmManager.cancel(pendingIntent);
        Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT).show();
    }

    private void showLog(String msg) {
        Log.d(TAG, msg);
    }
}

MyAlarm class:

package com.jimmytrivedi.alarmdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.provider.Settings;
import android.util.Log;

public class MyAlarm extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String id = intent.getStringExtra("REMINDER_ID");
        Log.d("Test", "ID: "+ id) ;
        MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);
        mediaPlayer.start();
        Log.d("Test", "Alarm just fired") ;
    }
}

In MainActivity i have one method setAlarm using that method i am calling intent for receiver class Don't know why intent is getting null? BTW, onReceived called and works other things. Reminder is also coming. And I printed logs also, This: (Log.d("Test", "Alarm just fired") ;) is also printing.

Why?

Any help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I just figured it out my problem. Use sendBroadcast(intent);

private void setAlarm(long time) {
        //creating a new intent specifying the broadcast receiver
         Intent intent = new Intent(this, MyAlarm.class);
         intent.putExtra("REMINDER_ID", "1");
         sendBroadcast(intent);

        //creating a pending intent using the intent
         pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        //setting the repeating alarm that will be fired every day
        alarmManager.setRepeating(AlarmManager.RTC, time, AlarmManager.INTERVAL_DAY, pendingIntent);
        Toast.makeText(this, "Alarm is set", Toast.LENGTH_SHORT).show();

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...