Android - BroadcastReceiver does not get user intentions

I have:

MyApp extends the application with onCreate:

sendBroadcast(refreshAlarm); Log.d(TAG, "broadcast sent with intent " + refreshAlarm); Log.d(TAG, "onCreate"); 

Where

 static final Intent refreshAlarm = new Intent(ACTION_REFRESH_RECEIVER); public static final String ACTION_REFRESH_RECEIVER = "com.example.myapp.REFRESH_RECEIVER"; 

BroadcastReceiver:

 package com.example.myapp; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.preference.PreferenceManager; import android.util.Log; public class RefreshReceiver extends BroadcastReceiver { private static final String TAG = "RefreshReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "broadcast received with intent " + intent); long interval = Long .parseLong(PreferenceManager.getDefaultSharedPreferences( context).getString("delay", "900")) * 1000; PendingIntent operation = PendingIntent.getService(context, -1, new Intent(context, RefreshService.class), PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(operation); if (interval > 0) { alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, operation); } Log.d(TAG, "onReceive: delay = " + interval); } } 

declared in expression:

 <receiver android:name="com.example.myapp.RefreshReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="com.example.myapp.REFRESH_RECEIVER" /> </intent-filter> </receiver> 

It seems to me that I have everything I need to do this job. The broadcast is sent to onCreate (I see that the log really is sent). Broadcast is declared with an intent filter to get refreshAlarm intent, but it does not receive it, and I cannot understand why. Do I need anything else?

+4
source share
5 answers

if you put BroadCastReceiver in mainfest.xml, you do not need to register it in the code, register it in the code only if you create it in your code

here is an example:

  <receiver android:name="MyReceiver" > <intent-filter> <action android:name="android.mybroadcast" /> </intent-filter> </receiver> 

and here to call it from your class file,

 Intent intent = new Intent(); intent.setAction("android.mybroadcast"); sendBroadcast(intent); 
+3
source

It is possible that it does not work, because your Receiver and the package name that you use to register in your manifest do not match.

Make sure your package name matches your package for your receiver.

+1
source

try registering the broadcast receiver programmatically

 public void registerBroadcastReceiver(View view) { this.registerReceiver(broadCastReceiver, new IntentFilter( "com.example.myapp.REFRESH_RECEIVER")); Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT) .show(); } 

and unregister

  public void unregisterBroadcastReceiver(View view) { this.unregisterReceiver(broadCastReceiver); Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT) .show(); } 
0
source

you said

MyApp Extends Application

you must register MyApp as an application in the android manifest.

put this line in android manifest application element

Android: name = "your.package.MyApp"

Where is your .pacakage - this is the package in which the MyApp file is located. Your application element should now look like this

 <application android:name="your.package.MyApp" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/yourTheme" > 
0
source

I had the same problem. Registered radio transmitters appear to work only for system activities. It worked with my user action only until my application was launched (activity was on the activity stack).

My workaround is to create an action for it. This does not work in the background, however it gives you the ability to capture events from other applications.

  <activity android:name=".ImportToProActivity"> <intent-filter> <action android:name="com.sourcecastle.logbook.ImportToProActivity"/> </intent-filter> </activity> Intent intent = new Intent(); intent.setComponent(new ComponentName("com.sourcecastle.freelogbook", "com.sourcecastle.logbook.ImportToProActivity")); startActivity(intent); 
0
source

Source: https://habr.com/ru/post/1498774/


All Articles