1

I am trying to use broadcastreceiver with alarm manager but unfortunately it doesnt work on my phone (android 10). broadcastReceiver doesnt initiliaze. if someone help me, I will appriciate him/her

BroadcastReceiver Class

`public class Alarm extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"Wake up",Toast.LENGTH_LONG).show();
        Log.i("fffffgggg", "onReceive: reached broadcast");
        MediaPlayer mediaPlayer=MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);
        mediaPlayer.start();
    }`

manifest for receiver

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".Alarm" android:exported="true" android:enabled="true">

        </receiver>
    </application>

MainActivity class

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


        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent notifyintent = new Intent(MainActivity.this, Alarm.class);
        PendingIntent pIntent = PendingIntent.getBroadcast(MainActivity.this, 0, notifyintent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
//update 30 seconds
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),30*1000, pIntent);
    }
Abdullah
  • 231
  • 2
  • 11

2 Answers2

0

You need to decide for action (some string), configure your broadcast receiver to process that action, and create a pending intent that fires that action.

It goes like this.

Manifest:

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".Alarm" android:exported="true" android:enabled="true">
           <intent-filter>
                <action android:name="MY_ACTION" />
            </intent-filter>
        </receiver>
    </application>

Main activity:

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


        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent notifyintent = new Intent("MY_ACTION");
        PendingIntent pIntent = PendingIntent.getBroadcast(MainActivity.this, 0, notifyintent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
//update 30 seconds
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),30*1000, pIntent);
    }

Also, it's recommended to make sure you got the right action in onReceive function.

Shlomi Katriel
  • 2,103
  • 1
  • 7
  • 20
  • Shlomi Katriel thanks for your reply but problem seems different here. adding action to intent is probably help to handle different or multiple broadcastreceivers. Now I tried 'alarm.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pIntent); ' it is working. I cant use setRepeating or setExacRepeating methods – Abdullah Mar 05 '21 at 19:15
  • Hi, I believe broadcast receivers only processes actions so you must set the action in the intent you fire and configure your manifest accordingly. About the other thing, have you tried `AlarmManager#setInexactRepeating`? See https://developer.android.com/training/scheduling/alarms#examples-of-elapsed-real-time-alarms – Shlomi Katriel Mar 05 '21 at 19:18
  • adding action may help to get spesific action in broadcastreceiver but look like it is not necessary to use – Abdullah Mar 05 '21 at 19:22
  • As far as I know it's a must-have – Shlomi Katriel Mar 05 '21 at 19:23
0

AlarmManager is using Pending Intent as base. Pending intent it is a intent modification so for the first you must define action in your manifest, and give that value to Pending intent. For more detail you can read alarm manager google documentation, I hope I've been of help.

Rob Gas
  • 56
  • 4
  • hi Rob Gas thanks for your answer. this issue is terribile. I found that battery optimization prevents setInexactRepeating and setRepeating working. when I disable battery optimization for my app and add REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission to manifest, I sarted to get messages from my broadcastReceiver repeatively https://www.debugcn.com/en/article/22807634.html article mentions the issue – Abdullah Mar 05 '21 at 20:48