Restart notification activity in Android
source link: https://www.codesd.com/item/restart-notification-activity-in-android.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Restart notification activity in Android
I have two Activities, MainActivityCaller and MainActivity, with activity MainActivityCaller starting activity MainActivity via the startActivity() method.
From a notification, I would like to launch activity MainActivity if it has been paused but exists in a task back stack (done with code below), but to launch MainActivityCaller if it does not (ie. if the MainActivity instance has been destroyed either by the user or the system).
MainActivity broadcasts the following when the user's location changes
@Override
public void onLocationChanged(Location location) {
final Location finalLocation = location;
final Intent restartMainActivity = new Intent(this, MainActivity.class);
sendOrderedBroadcast(
new Intent(LOCATION_CHANGED_ACTION),
null,
new BroadcastReceiver() {
@TargetApi(16)
@Override
public void onReceive(Context context, Intent intent) {
if (getResultCode() != RESULT_OK) {
PendingIntent pi = PendingIntent.getActivity(context, 0, restartMainActivity, 0);
Notification.Builder nb = new Notification.Builder(context)
.setAutoCancel(true)
.setContentText("Lat = " + Double.toString(finalLocation.getLatitude()) + "\nLong = " + Double.toString(finalLocation.getLongitude()))
.setContentIntent(pi)
.setSmallIcon(android.R.drawable.stat_sys_warning));
NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, nb.build());
}
}
},
null,
0,
null,
null);
}
MainActivity also has a broadcast receiver instantiated in its onCreate method (abbreviated version below)
@Override
protected void onCreate(Bundle savedInstanceState) {
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (isOrderedBroadcast())
setResultCode(RESULT_OK);
}
};
}
With the receiver being registered in the onResume method
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(LOCATION_CHANGED_ACTION);
registerReceiver(mReceiver, intentFilter);
}
And unregistered in the onPause method
@Override
protected void onPause() {
super.onPause();
if (mReceiver != null) {
unregisterReceiver(mReceiver);
}
}
In the Manifest file, MainActivity is declared to only be launchable in one task
<activity android:name=".MainActivity"
android:launchMode="singleTask" />
Right now this either creates or restarts MainActivity (depending on if MainActivity was destroyed or stopped). How can this be modified to launch MainActivityCaller when no instance of MainActivity exists in any task back stack?
Thanks!
You'll want to register your broadcast receiver in AndroidManifest.xml, make the receiver be a public Java class, and have the receiver (re)start the activity.
In your AndroidManifest.xml:
<receiver
android:name=".RestarteActivityBR"
android:exported="false">
<intent-filter>
<action android:name="com.example.intent.restart" />
</intent-filter>
</receiver>
And in RestartActivityBR.java:
public class RestartActivityBR extends BroadcastReceiver
{
@Override
public void onReceive(final Context context, final Intent intent)
{
Intent main = new Intent(context, AppMain.class);
main.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(main);
}
}
Recommend
-
106
page.description
-
40
readme.md grace
-
38
README.md composer/xdebug-handler
-
9
Restart ID sequences in Postgres on Truncation Mar 13, 2020 Note: This is from a real world thing that I'm about to release. Like a lot of database apps in the world, mine generally te...
-
2
When You Can't Run Rails Console Restart Spring Feb 2, 2020 So I just hit this little gem of a pissant thing (yep -...
-
12
How to restart your Azure workers in less than a minute Oct 29, 2009
-
13
Click the notification to not start a specific activity advertisements I am using a notification where I added an "action" which should start...
-
4
Android 12: How to prevent activity restart on changing phone wallpaper? The
-
10
Auto Restart application after Crash/Force Close in Android Byadmin April 27, 2015April 28, 2015 ...
-
12
@victorbrndlsVictor BrandaliseI like understanding how things work and why they work li...Credibility
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK