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.
Related
I have a android compose application and broadcast not working with alarm.
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<receiver android:name=".broadcast.EventAlarmBroadcastReceiver"
android:exported="false"
android:process=":remote"
android:enabled="true">
<intent-filter>
<action android:name="com.zxy.control_time.app.REMIND_ACTION"/>
</intent-filter>
</receiver>
kotlin code , setExact function is not working
val intent = Intent(context,EventAlarmBroadcastReceiver::class.java)
intent.`package` = BuildConfig.APPLICATION_ID
val pendingIntent = PendingIntent.getService(
context,
EventAlarmBroadcastReceiver.REMIND_INTENT_ID_BASIC_NUMBER + this.id,
intent,
0
)
// alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + 5 * 1000,pendingIntent) not working
// context.sendBroadcast(intent) working fine
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
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>
I have one class called StopAlarmReceiver and another BootHandler.
The BootHandler resets the alarm after reboot, which was set before the boot. My problem is that the BootHandler sets the alarm but not getting fired.
Entries in Manifest file.
<receiver
android:name="com.sign.android.myscheduler.app.StopAlarmReceiver"
android:enabled="true"
android:exported="true" >
</receiver>
<receiver
android:name="com.sign.android.myscheduler.app.BootHandler"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Code in BootHandler.
AlarmManager mgr= (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, StopAlarmReceiver.class);
PendingIntent pi = PendingIntent.getService(context, 1, i, 0);
long time = sharedPreferences.getLong("Old_time", 0);
Log.e(TAG, "New time: " +new Date(SystemClock.elapsedRealtime() + time));
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + time, 5000, pi);
manifest
just write two receivers 1 for alarm and 1 for handle the boot
<receiver
android:name="packagename.AlarmReceiver"
>
</receiver>
<!-- Will not be called unless the application explicitly enables it -->
<receiver android:name="com.avion.contact_app.DeviceBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
add following permissions
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
In DeviceBootReceiver class check the intent action first
#Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = context.getSharedPreferences(
"DefaultReminder", context.MODE_PRIVATE);
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
//do whatever you want after reboot or fire alarm again
}
}
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