5

Restart notification activity in Android

 2 years ago
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.
neoserver,ios ssh client

Restart notification activity in Android

advertisements

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK