Resume background activity on notification

半腔热情 提交于 2019-12-07 00:45:33

What you need is just a simple Activity that decides what to do. Here is an example:

public class NotificationActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Check if the app was already running
        if (isTaskRoot()) {
            // App wasn't running, so start the app from the beginning
            Intent startIntent = new Intent(this, MyStartingActivity.class);
            startActivity(startIntent);
        } else {
            // App was already running, bring MainActivity to the top
            Intent reorderIntent = new Intent(this, MainActivity.class);
            reorderIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(reorderIntent);
        }
        // We are done now so just finish
        finish();
    }
}

Set up your notification to start this activity. Make sure that in the manifest the task affinity of this activity is the same as the task affinity of the other activities in your application (by default it is, if you haven't explicitly set android:taskAffinity).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!