Why onReceive in receiver not be invoked? - android

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

Related

i have a problem in my BroadcastReceiver, after the device is reboot receiver is not called

my program extends From BroadCastReciever to set Alarm, after the device is reboot receiver is not called, this is my code
1- BroadcastReceiver
public class myReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()!=null) {
if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
Log.d("myActivity","repooted");
// alarm settings
}
}
}
2-xml
<receiver
android:name=".sync.myReceiver"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>
In your AndroidManifest.xml have you added permission to receive boot complete?
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

use Broadcast Receiver to catch incoming call, onReceive not triggered? Log cannot be printed out

In broadcastReceiver:
public void onReceive(final Context context, Intent intent) {
Log.e("Current:","entered111!");
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
Intent intent1=new Intent(context,myService.class);
intent1.putExtra("incomingNumber",incomingNumber);
context.startService(intent1);
}
}
This is my Manifest file
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".myService" />
<receiver android:name=".ServiceReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
You have to add below permission in your manifest file;
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
Then enable 'Phone' in 'Permissions' of your application in 'Settings' -> 'Apps' -> Application -> 'Permissions'.
Thank you.

BroadcastReceiver not receiving any data

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>

Android: How to make a single Receiver and switch between intents?

I have two receivers
<receiver
android:name=".BootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".DateReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
</receiver>
This are their java implementations
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("BootReceiver", "ok");
}
}
public class DateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("DateReceiver", "new date");
}
}
I'd like to make a single receiver with all intent-filters and make a switch when it receives an intent.
<receiver
android:name=".DataAndBootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
</receiver>
public class DataAndBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()
.equals("android.intent.action.DATE_CHANGED")) {
Log.i("Receiver", "Data");
} else {
Log.i("Receiver", "Boot");
}
}
}
Add it in manifest like this
<receiver android:name=".EventHandler" >
<intent-filter>
<action android:name="android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT" />
<action android:name="android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED" />
<action android:name="android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED" />
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<action android:name="android.intent.action.HEADSET_PLUG"/>
<action android:name="android.hardware.action.NEW_PICTURE" />
<action android:name="android.hardware.action.NEW_VIDEO" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
and receive them something like that....
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "---------------------------------------");
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_AIRPLANE_MODE_CHANGED)){
// Do Some Thing here...
}
}

Custom GCMBroadcastReceiver

I currently create a broadcastreceiver like this:
In my fragment:
receiver=new GcmBroadcastReceiverLobby();
IntentFilter filter =new IntentFilter("com.google.android.c2dm.intent.RECEIVE");
filter.setPriority(9999);
filter.addAction("com.google.android.c2dm.intent.RECEIVE");
getActivity().registerReceiver(receiver, filter);
class GcmBroadcastReceiverLobby extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String messageType = extras.toString();
setResultCode(Activity.RESULT_CANCELED);
abortBroadcast();
}
}
The problem is intent.getExtras doesn't return the details from the GCM-Message.
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="de.battlestr1k3.gamelobbies" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" android:exported="true" />
The GcmBroadcastReceiverLobby has a higher priority so it catches the notification first. I might be missing a permission in the code?
extras.toString()
returns Bundle[mParcelledData.dataSize=280]
Ok I added
if (!extras.isEmpty()) {
}
and it worked

Categories

Resources