BroadcastReceiver isn't receiving ACTION_SCREEN_OFF intent - java

I have created a BroadcastReceiver that should listen for the "ACTION_SCREEN_OFF" intent.
public class ExampleReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// run a service..
}
}
I have registered it in the Manifest:
<receiver android:name=".path.to.ExampleReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SCREEN_OFF"></action>
</intent-filter>
</receiver>
But when the screen is turned off, the onReceive() method is not called. What else do I need to do to get this functionality?

ACTION_SCREEN_OFF is not sent to receivers registered in the manifest. It is only sent to receivers registered via registerReceiver() from a running component.

Related

App Service won't Start after reboot by using BroadCastReceiver

I want my service to be started when android is rebooted. I used BroadcastReceiver to do so.
public class autostart extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
MyService1 mYourService = new MyService1();
MainActivity.instance_main.showToast("BroadCast Received...................");
Intent mServiceIntent = new Intent(MainActivity.instance_main, mYourService.getClass());
if (!MainActivity.instance_main.isMyServiceRunning(mYourService.getClass())) {
MainActivity.instance_main.startService(mServiceIntent);
MainActivity.instance_main.showToast("Restarted...................");
}
else {
MainActivity.instance_main.showToast("already running..................");
}
}
}
Receiver in Android Manifest
<receiver android:name=".autostart" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
Permissions defined
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Battery optimization ignore permission also requested in OnCreate()
Intent intent=new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:"+MainActivity.instance_main.getPackageName()));
MainActivity.instance_main.startActivity(intent);
But it won't work. Service won't start on reboot. Kindly Suggest me solution.
WE need to register the receiver. Add the following code to OnCreate()
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mService=new Autostart();
registerReceiver(mService,filter);
However after reboot it wont automatically hit.

My FragmentActivity is not receiving broadcasts

I registered a BroadcastReceiver in my FragmentActivity like so:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
setMapLocations(intent);
}
};
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
setMapLocations(intent);
}
};
and start the receiver is onStart():
public void onStart() {
super.onStart();
LocalBroadcastManager.getInstance(this).registerReceiver((mMessageReceiver),
new IntentFilter("MyNotificationData"));
}
and my broadcast sender is defined in the manifest as follows:
<service
android:name=".TrackerMessagingService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
The problem is that onReceive() never gets hit, even though the exact same code worked fine in an AppCompatActivity in the same app. Any idea what's going on? Do broadcast services not play well with FragmentActivity?
I don't know why this worked, but I was able to get around the issue by leaving the BroadcastReceiver code in the AppCompatActivity. In that case, both BroadcastReceivers got hit.

BroadcastReceiver not receiving boolean value after AlarmManager finishes set time

I am using a BroadcastReceiver to enable a button when it is triggered. I have an AlarmManager that runs for a certain amount of time. When the time has elapsed, it sends a broadcast like this:
Intent i = new Intent("polarCap1Status");
i.putExtra("polarCap1Stat", true);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);
I use this code to receive the broadcast:
//broadcast receiver to allow button to be clicked again
mMessageReceiver1 = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
clickOnOff1 = intent.getBooleanExtra("polarCap1Stat", false);
polarCap1.setEnabled(clickOnOff1);
updateScores();
resources.edit().putBoolean("pc1Set", false).commit();
}//end onReceive function
};
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(mMessageReceiver1, new IntentFilter("polarCap1Status"));
I have the receiver setup in my manifest like so:
<receiver android:name="com.twentytwentythree.sab.SetupTimerPC1" >
<intent-filter>
<action android:name="polarCap1Status" />
</intent-filter>
</receiver>
SetupTimerPC1 is the name of the class that is sending the broadcast. It is sending it to another class called runGraphics.
Please let me know if you need any more information.
Edit:
I added an intent-filter to the receiver in the manifest but it has not changed anything.

OnBootReceiver works, but doesn't find AlarmReceiver

I'm trying to set up an alarm receiver right after booting. Therefore, i have an OnBootReceiver that should register the alarm. The onBootReceiver works and it gets called, but somehow it cannot find my AlarmReceiver class.
OnBootReceiver which succesfully starts after booting:
public class OnBootReceiver extends BroadcastReceiver {
private static final String TAG = "OnBootReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "called");
Intent i = new Intent(context, com.packagenames.AlarmReceiver.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, 30);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), time.getTimeInMillis(), pi);
}
}
As you you can see, it configures the alarm tries to invoke com.packagenames.AlarmReceiver.class. This class exists and is located in the same package:
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "alarm received");
Intent i = new Intent(context, com.packagename.DataService.class);
i.putExtra("action", "process");
context.startService(i);
}
}
Unfortunately, i get the following error:
02-03 09:22:25.344: W/ActivityManager(103): Unable to start service
Intent { flg=0x4 cmp=com.phonegap.packagename/.AlarmReceiver (has extras)
}: not found
The Android Manifest looks like this
<application>
// activities etc
<receiver
android:name="com.phonegap.packagename.OnBootReceiver"
android:enabled="true"
android:exported="false"
android:label="OnBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name="com.phonegap.packagename.AlarmReceiver"
android:enabled="true"
android:label="AlarmReceiver">
<intent-filter>
</intent-filter>
</receiver>
</application>
Do you see a mistake? Maybe I forgot something?
Thanks
edit: in the manifest, I added
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
in order to make the OnBootReceiver work. Do I need something similar for the alarm?
Shouldn't you use getBroadcast instead of getService when creating a pending intent ?
The whole receiver stuff ONLY works if you application is NOT installed on a SD card. Add this to your manifest file to do so:
android:installLocation="internalOnly"

How to invoke an activity on SMS receive in Android?

My application is receiving an SMS and I want to invoke an activity on receiving that SMS. How do I do this?
create receiver like this
<receiver android:name=".BroadcastServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
</intent-filter>
</receiver>
in class do whatever you want
public class BroadcastServiceReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// do you want
}
}

Categories