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
Related
I have a several devices (Minix X68-i) all running the same firmware and software versions.
On some of them my boot receiver works fine, others it rarely works, and a few it's closer to 50-50 whether they will receive the event.
I'm out of ideas as to what could cause the code to work so erratically, other than potentially faulty hardware.
Manifest:
<receiver android:enabled="true" android:exported="true"
android:name="com.proreception.displaymanagement.Receiver.BootReceiver"
android:label="StartMyActivity" >
<intent-filter android:priority="500" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
BootReceiver
public class BootReceiver extends BroadcastReceiver {
private static final String TAG = "BootReceiver";
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) && constants.ONBOOT) {
Intent i = new Intent(context, FullscreenActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d(TAG, "Starting Application");
context.startActivity(i);
}
}
}
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>
I would like to build a boot receiver.
Manifest.XML
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
And this is my Receiver Class:
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.e("-->", "BOOT COMPLETED");
}
}
If i restart my device i get the message (on my device):
the app xxx was closed
Try to change this in your .manifest
<receiver android:name=".BootReceiver"
android:enabled="true"
android:exported="false"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Also Try this out in your Reciever
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "Receiver Called", Toast.LENGTH_LONG).show();
}
}
Also Add android:persistent="true" in to your manifest tag
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
I am using broadcast receivers for performing some action when the device is switched off and switched on again,but they are not working.These are the receivers in manifest file:
<receiver android:name=".ShutdownReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
<receiver android:name=".RestartReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
These are the corresponding classes:
public class ShutdownReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.d("In","Switched Off");
}
}
public class RestartReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
SecureMessagesActivity.ToDoOnMobileSwitchOn();
Log.d("In","Switched On");
}
}
Please help me.Thanks in advance.
Be sure to request the RECEIVE_BOOT_COMPLETED permission in your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>