How to bring Android existing activity to front via notification

后端 未结 4 2030
醉酒成梦
醉酒成梦 2020-12-06 10:46

I have one Android application, when it runs a service, I want to show the notification on the status bar. Then the user could navigate to other application by pressing HO

相关标签:
4条回答
  • 2020-12-06 11:25

    When an Activity is in the background, Android may kill the activity at any time to free resources. See the Activity documentation for complete details on the lifecycle.

    Your activity needs to be prepared to save its state in the onPause or onSaveInstanceState methods. The document referenced above has additional details on saving persistant state.

    0 讨论(0)
  • 2020-12-06 11:26

    I'd recommend you do this,

    private void showNotification () 
    {
    
    Intent toLaunch = new Intent(getApplicationContext(), DummyActivity.class);
    
    // You'd need this line only if you had shown the notification from a Service
    toLaunch.setAction("android.intent.action.MAIN");
    
    PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);
    
    .... 
    }
    

    The DummyActivity should simply be an activity that always finishes itself in the oncreate event.

    In the manifest file, add these lines

    <activity class=".DummyActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>
    

    I hope this helps ...

    0 讨论(0)
  • 2020-12-06 11:28
    private void showNotification() {
        Intent toLaunch = new Intent(getApplicationContext(), MySingleTopActivity.class);
    
        //add these two lines will solve your issue
        toLaunch.setAction("android.intent.action.MAIN");
        toLaunch.addCategory("android.intent.category.LAUNCHER");
    
        PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0, toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);
    
        notification.setLatestEventInfo(getApplicationContext(), getText(R.string.GPS_service_name), text, intentBack);
        ...
    }
    
    0 讨论(0)
  • 2020-12-06 11:32

    Another way to launch your package intent.

    private void NotificationwithLaucherSelfPackage(Context context , int notification_id){
            Notification noti = new Notification.Builder(context)
                    .setContentTitle("Your Title")
                    .setContentText("Your Text")
                    .setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha)
                    .setContentIntent(PendingIntent.getActivity( context ,notification_id , getLauncherIntent(context) , PendingIntent.FLAG_UPDATE_CURRENT))
                    .build();
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        }
    
    private Intent getLauncherIntent(Context context){
            return context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        }
    
    0 讨论(0)
提交回复
热议问题