Not receiving broadcast form IntentSender - android

I'm trying to get a broadcast when the user select in a chooser as stated What is the purpose of IntentSender? and Get IntentSender object for createChooser method in Android.
I create the chooser as describer in both post:
Intent intent = new Intent(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
.setType("text/plain");
Intent receiver = new Intent(this, BroadcastTest.class)
.putExtra("test", "test");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent chooser = Intent.createChooser(intent, "test", pendingIntent.getIntentSender());
startActivity(chooser);
and register my BroadcastReceiver in my AndroidManifest:
<receiver
android:name="com.migore.intentsender.BroadcastTest"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1">
<action android:name="com.migore.intentsender.intent.TEST" />
</intent-filter>
</receiver>
However, my receiver is never called. I already tried registering the receiver in the code but it also didn't work. What am I missing?

Hello use bellow code
<receiver
android:name="com.migore.intentsender.BroadcastTest"
android:enabled="true"
android:exported="true">
<intent-filter>
<action droid:name="android.intent.extra.CHOSEN_COMPONENT" />
</intent-filter>
</receiver>

Related

Android - Adding an Intent to a notification

I'm new on android development and I still don't understand Intent very well. Here i'm using Firebase to create notifications, I add an Intent to my notification, but when my app is in foreground and I click on the notification nothing happens (when the app is killed or in background it work well).
The strange thing is at a moment it was working, when I clicked on the notification the function "onNewIntent" of my "MainActivity" class was called, but now nothing happen anymore, and I think I changed nothing on the code.
Here is how I create my intent :
Notifications notifs = new Notifications(this);
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
String url = remoteMessage.getData().get("url");
Intent intentNotif = new Intent(this, MainActivity.class);
intentNotif.setAction(Intent.ACTION_MAIN);
intentNotif.addCategory(Intent.CATEGORY_LAUNCHER);
intentNotif.putExtra("url", url);
intentNotif.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//intentNotif.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notifs.showNotif(title, body, true, intentNotif);
Here is how I add the intent to the notification :
this.builderGeneric.setContentIntent(PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
I also tried to create an intent with a custom action but it's not working, I tryed different intent flags, but I feel like i'm trying random things without knowing what I do, so that's why I'm asking for your help.
EDIT :
Here is how I create the notification if it's usefull :
Notifications(Context context) {
this.context = context;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.notifManager = context.getSystemService(NotificationManager.class);
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
channel.setDescription(CHANNEL_DESCRIPTION);
channel.enableLights(false);
channel.enableVibration(false);
channel.setSound(null, null);
if (this.notifManager != null) {
this.notifManager.createNotificationChannel(channel);
}
} else {
this.notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
this.builderGeneric = new Notification.Builder(context)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher_round);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.builderGeneric.setChannelId(this.CHANNEL_ID);
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
this.builderGeneric.setSmallIcon(R.mipmap.ic_launcher_foreground);
}
}
public void showNotif(String title, String text, boolean cancelable, Intent intent) {
this.builderGeneric.setContentTitle(title)
.setContentText(text)
.setOngoing(!cancelable)
.setContentIntent(PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
Notification notifGeneric = this.builderGeneric.build();
this.notifManager.notify(1, notifGeneric);
}
EDIT 2 : Here is my manifest :
<application
android:allowBackup="true"
android:fullBackupContent="#xml/backup_descriptor"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:launchMode="standard"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
<receiver
android:name=".NetworkChangeReceiver"
android:label="NetworkChangeReceiver">
<intent-filter
android:name=".NetworkIntentFilter"
android:label="NetworkIntentFilter">
<action
android:name="android.net.conn.CONNECTIVITY_CHANGE"
tools:ignore="BatteryLife" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
<service android:name=".MyFirebaseService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
OK, First try to remove
intentNotif.setAction(Intent.ACTION_MAIN);
intentNotif.addCategory(Intent.CATEGORY_LAUNCHER);
Next, adding android:launchMode="singleTask" (also removing android:launchMode="standard") inside your activity tag in AndroidManifest.xml.
Then try again, please aware that with the launchMode is singleTask and if you click on your notification while your MainActivity is opened -> onNewIntent will be triggered (because Android will not create one more instance of this Activity) otherwise onCreate will be called.
And if it works, I would like to recommend you to read more about LaundMode here

Android: Google Fit: Sessions Start/End BroadcastReceivers Not Triggering

I want to be able to start a BroadcastReceiver when a Google Fit session starts or ends on a phone. I have the manifest set up for the receivers:
<receiver android:name=".YogaSessionStartedBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.fitness.session_start" />
<data android:mimeType="vnd.google.fitness.activity_type/yoga" />
</intent-filter>
</receiver>
<receiver android:name=".YogaSessionEndedBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.fitness.session_end" />
<data android:mimeType="vnd.google.fitness.activity_type/yoga" />
</intent-filter>
</receiver>
And I'm starting a session with activity type YOGA in my app:
mSession = new Session.Builder()
.setName(SESSION_NAME)
.setIdentifier(getString(R.string.app_name) + " " + System.currentTimeMillis())
.setDescription("Yoga Session Description")
.setStartTime(Calendar.getInstance().getTimeInMillis(), TimeUnit.MILLISECONDS)
.setActivity(FitnessActivities.YOGA)
.build();
PendingResult<Status> pendingResult =
Fitness.SessionsApi.startSession(mGoogleApiClient, mSession);
However onReceive is never called in my BroadcastReceivers. Any suggestions?
Have you Register for Session ?
registerForSessions (GoogleApiClient client, PendingIntent intent)
Something like this:
Intent intent = new Intent(mContext,YogaSessionEndedBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, alarmIntent, 0);
Fitness.SessionsApi.registerForSessions(mClient,pendingIntent);
Which mClient is your GoogleApiClient,And mContext is your Activity Context.

Android app doesn't start at system boot

I've a reciever
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myStarterIntent = new Intent(context, MainActivity.class);
context.startActivity(myStarterIntent);
}
}
and have modified the AndroidManifest.xml, adding these lines
<receiver
android:enabled="true"
android:name=".MyBroadcastReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
to section.
The application still does not start on system boot..any ideas appreciated. At least how can I monitor what is going on after device reboot (because I cant just use breakpoints in that case)
You need to provide Intent.FLAG_ACTIVITY_NEW_TASK flag when starting activity from BroadcastReceiver.
Intent myStarterIntent = new Intent(context, MainActivity.class);
myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
This is how you can test the BroadcastReceiver.

BroadcastReceiver is not able to receive the Intent

I want to write a BroadcastReceiver to receive the application install action. But it failed, so I test if my receiver is well or not. So custom a intent, it also filed. below is my code. Please help me correct it.
public class MyInstallReceiver extends BroadcastReceiver {
// public MyInstallReceiver() {
// }
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
Log.d("receiver", "Intent Detected");
if (intent.getAction (). equals ("android.intent.action.PACKAGE_ADDED")) {
String packageName = intent.getDataString ();
//System.out.println ("installed:" + packageName + "package name of the program");
Log.d("receiver","installed:" + packageName + "package name of the program");
}
}
}
custom intent
public void installAPK(View v){
startActivity(intent);
Intent intent = new Intent();
intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
sendBroadcast(intent);
Log.d("receiver", "Intent sent");
}
Manifest.xml
<receiver
android:name=".MyInstallReceiver"
android:enabled="true"
android:exported="true" >
<Intent-filter>
<action android:name = "android.intent.action.PACKAGE_ADDED"/>
<action android:name = "android.intent.action.PACKAGE_REMOVED"/>
<action android:name="com.tutorialspoint.CUSTOM_INTENT">
</action>
<Data android:scheme = "package" />
</Intent-filter>
</receiver>
enter code here
I don't know about correct spelling in your manifest, but this code is definitely works very well:
<receiver android:name=".MyInstallReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
Every application install/uninstall will trigger this receiver.
Everythong looks good, expect a typo in your manifest. It should be <intent-filter> and not <Intent-filter>

Android Intent broadcast from am broadcast

Can anyone help with this code:
Intent localIntent = new Intent("android.intent.action.MULTI_MODE_CHANGED ");
localIntent.putExtra("MULTI_MODE", paramString);
mContext.sendBroadcast(localIntent);
I am trying to understand how to broadcast the same intent from am broadcast from shell.
mContext is set to null at the beginning but on the onCreate function, this is displayed:
mContext = this;
This is in the AndroidManifest.xml:
<receiver android:name="HiddenmenuBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE"/>
<data android:host="LTEMODE" android:scheme="android_secret_code"/>
</intent-filter>
</receiver>
Thanks,
Alex

Categories

Resources