BroadcastReceiver not receiving any data - android

I use this code to send data to my BroadcastReceiver.
Log.d("recive message message message", message);
Intent resultBroadCastIntent = new Intent();
resultBroadCastIntent.setAction(Intent.ACTION_SEND);
resultBroadCastIntent.addCategory(Intent.CATEGORY_DEFAULT);
resultBroadCastIntent.putExtra(OUTPUT_TEXT, message);
sendBroadcast(resultBroadCastIntent);
and the BroadcastReceiver code
public class Broadcast_Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, Notification_Intent_Service.class));
String resultText =intent.getStringExtra(Notification_Intent_Service.OUTPUT_TEXT);
Log.d("recive dwwwwwwwwwwwwwwww", resultText);
// this never run when message arrive
}
}
and this is my manifest
<receiver
android:name=".Service.Broadcast_Receiver"
android:enabled="true"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
now in
Log.d("recive message message message", message);
the message is printed and I see it but it wasn't sent to the BroadcastReceiver or is it that the BroadcastReceiver didn't receive the data? I'm not sure where the problem is.

You can use the BOOT_COMPLETED id when sending a broadcast
Intent resultBroadCastIntent = new Intent("android.intent.action.BOOT_COMPLETED");
resultBroadCastIntent.putExtra(OUTPUT_TEXT, message);
sendBroadcast(resultBroadCastIntent);
or Specify SEND action in <intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SEND" />
</intent-filter>
Note: Make sure your BroadcastReceiver isn't an Inner class otherwise add static into it or move it in upper level.

You need to register Intent.ACTION_SEND for Broadcast receiver in manifest
<receiver
android:name=".Service.Broadcast_Receiver"
android:enabled="true"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SEND" />
</intent-filter>
</receiver>

Related

Can not start on boot when add more broadcast receive

I found many topic but no case fit for my question.
The first my app working smooth, it is always startup with device by code bellow:
In AndroidManifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and receiver:
<!-- start when module start broadcast -->
<receiver android:name=".StartOnBoot"
android:exported="true">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<!-- listen messenger received -->
<receiver
android:name=".Phone.SmsListener"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
And class StartOnBoot
public class StartOnBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent serviceIntent = new Intent(context, MainActivity.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
}
} }
And now I added 2 broadcast to listen state of SD card and SIM card. But when I finish, My app can not start when my device restarted. And I don't know, where is problem, nothing clue.
<!-- broad cast SDCard receive-->
<receiver android:name=".Peripheral.SDCardListener">
<intent-filter>
<action android:name="android.intent.action.MEDIA_EJECT" />
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
</receiver>
<receiver android:name=".Phone.SimCardReceiver">
<intent-filter>
<action android:name="android.intent.action.SIM_STATE_CHANGED"/>
</intent-filter>
</receiver>
And 2 classes receiver:
public class SDCardListener extends BroadcastReceiver {
//restart app when SD card mounted
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG","SDcard state : " + intent.getAction());
if (intent.getAction().equals(Intent.ACTION_MEDIA_EJECT)){
//sdcard eject
}else if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)){
SDCard.getInstance().SDCardMounted();
}
}
}
for sim state:
public class SimCardReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG", "Sim state :" + intent.getAction());
}
}
My device target is only Android 7.1, SDK lv 25.
Some methods I tried but not work:
I removed SimCardReceiver
I added "android:priortity to StartBoot" like this:
<intent-filter android:priority="1000">
But still not working, Remember, my app always work without 2 broadcast SDCardListener and SimCardReceiver
Extra question:
How many broadcast are available to use in one app. How many is good ? one or not limit
Could I merge all receiver to one broadcast

Widget issue: BroadcastQueue: Background execution not allowed: receiving Intent

My app widget stops working after upgrading to targetSDk to 28.
It is flawlessly working on old targetsdk devices.
I am getting the following error:
W/BroadcastQueue: Background execution not allowed: receiving Intent { act=ch.corten.aha.worldclock.WIDGET_DATA_CHANGED flg=0x10 } to ch.corten.aha.worldclock/.WorldClockWidgetProvider
W/BroadcastQueue: Background execution not allowed: receiving Intent { act=ch.corten.aha.worldclock.WIDGET_DATA_CHANGED flg=0x10 } to ch.corten.aha.worldclock/.WeatherWidgetProvider
androidmanifest.xml file contents are given below-
<!-- clock widget -->
<receiver
android:name=".WorldClockWidgetProvider"
android:exported="false"
android:label="#string/clock_widget_name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
</intent-filter>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
</intent-filter>
<intent-filter>
<action android:name="ch.corten.aha.worldclock.WIDGET_DATA_CHANGED" />
</intent-filter>
<intent-filter>
<action android:name="ch.corten.aha.worldclock.CLOCK_TICK" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/world_clock_appwidget_info" />
</receiver>
<receiver
android:name=".WeatherWidgetProvider"
android:enabled="#bool/enable_weather_widget"
android:exported="false"
android:label="#string/weather_widget_name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
</intent-filter>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
</intent-filter>
<intent-filter>
<action android:name="ch.corten.aha.worldclock.WIDGET_DATA_CHANGED" />
</intent-filter>
<intent-filter>
<action android:name="ch.corten.aha.worldclock.CLOCK_TICK" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/weather_appwidget_info" />
</receiver>
And my reviever class code(
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (WIDGET_DATA_CHANGED_ACTION.equals(intent.getAction())
|| CLOCK_TICK_ACTION.equals(intent.getAction())) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm.isScreenOn()) {
onClockTick(context);
}
}
}
Where clock widget provider is extending AppWidgetProvider.
world clock activity
private static void sendWidgetRefresh(Context context) {
// send update broadcast to widget
Intent broadcast = new Intent(ClockWidgetProvider.WIDGET_DATA_CHANGED_ACTION);
context.sendBroadcast(broadcast);
}
project link for reference. Followed previous posts but did not worked.
Oreo - Widget services and broadcast receiver: Not allowed to start service Intent
The problem is in implicit broadcast that you're making in sendWidgetRefresh.
You can break through the ban by defining a component name in your intent.
private static void sendWidgetRefresh(Context context) {
// send update broadcast to widget
Intent broadcast = new Intent(ClockWidgetProvider.WIDGET_DATA_CHANGED_ACTION);
ComponentName componentName = new ComponentName(context, WorldClockWidgetProvider.class);
broadcast.setComponent(componentName);
context.sendBroadcast(broadcast);
}
This worked for me!
Change your implicit intent to explicit intent, because starting Oreo implicit intents don't run in background!
https://developer.android.com/about/versions/oreo/background.html

Multiple actions registered on one broadcast Receiver is not working in android

I registered multiple intent under one receiver
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
android:name="com.test.callerpopup.CallerPopUp"
android:enabled="true"
android:exported="true">
<intent-filter >
<action android:name="android.intent.action.REBOOT" ></action>
<action android:name="android.intent.action.BOOT_COMPLETED" ></action>
<action android:name="android.intent.action.PHONE_STATE" ></action>
</intent-filter>
In my Broadcast receiver class
#Override
public void onReceive(final Context context, Intent intent) {
String phone_state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (phone_state.equals(TelephonyManager.EXTRA_STATE_RINGING) ){
Toast.makeText(context, "Incoming Call", Toast.LENGTH_LONG).show();
}
Normal execution of app is working fine but when i am restarting phone, the toast is not working . Why ? I have registered the receiver on "REBOOT" and "BOOT_COMPLETED"

Why onReceive in receiver not be invoked?

I have declared receivers in Manifest.xml like this:
<receiver android:name=".receivers.MyTrackerReceiver"
android:exported="true">
<intent-filter android:priority="998">
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:exported="true"
android:permission="android.permission.INSTALL_PACKAGES">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
and my custom receiver is
public class MyTrackerReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String referrerString = extras.getString("referrer");
Log.v("referrer ", referrerString);
SharedPreferenceUtils.getInstance(context).setReferrer(referrerString);
new CampaignTrackingReceiver().onReceive(context, intent);
}
}
but the onReceive in CampaignTrackingReceiverdoesn't get called, what I am missing,thanks!
It's working now there was some problem with sending broadcast to receiver

How to launch the application upon booting up the device?

Can anybody share the sample code to how to launch android application upon starting/booting up the device?
This code will launch an application on start up. You need to listen for ACTION_BOOT_COMPLETE.
in AndroidManifest.xml (application-part):
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
[..]
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
[..]
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}

Categories

Resources