0

基本上,我正在浏览互联网或查看其他应用。我收到一条通知,打开我的应用程序。我做了一些东西,它完成。我的应用程序关闭,我回到了启动器。我如何让我的应用程序完成并将我返回到之前浏览过的任何应用程序/ Chrome页面?Android - 当我的应用完成后恢复后台应用

AndroidManifest节这个活动:

<activity 
     android:name=".AcceptRejectActivity" 
     android:label="@string/title_activity_accept_reject" 
     android:screenOrientation="portrait" > 
    </activity> 

通知码:

mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent resultIntent = new Intent(this, AcceptRejectActivity.class); 
    resultIntent.putExtra("message","Do you choose to accept this message?");  

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
    stackBuilder.addParentStack(AcceptRejectActivity.class); 

    // Adds the Intent that starts the Activity to the top of the stack // 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent contentIntent = 
      stackBuilder.getPendingIntent(
        0, 
        PendingIntent.FLAG_UPDATE_CURRENT 
      ); 
    String msg = "You have a message" 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.ic_logo) 
        .setContentTitle("Message") 
        .setStyle(new NotificationCompat.BigTextStyle() 
          .bigText(msg)) 
        .setContentText(msg) 
        .setPriority(Notification.PRIORITY_HIGH); 
    mBuilder.setContentIntent(contentIntent); 
    mBuilder.setAutoCancel(true); 

    android.app.Notification note = mBuilder.build(); 
    note.defaults |= android.app.Notification.DEFAULT_VIBRATE; 
    note.defaults |= android.app.Notification.DEFAULT_SOUND;   

    mNotificationManager.notify(0, note); 

片段的关闭功能

private void allDone() { 
     Log.i(TAG, "The choice has been made");    
     getActivity().finish(); 
     //NavUtils.navigateUpFromSameTask(getActivity()); 
    } 

回答

0

我设法解决了我的问题。

Android清单:

<activity 
     android:name=".AcceptRejectActivity" 
     android:label="@string/title_activity_accept_reject" 
     android:screenOrientation="portrait" 
     android:launchMode="singleInstance" 
     android:excludeFromRecents="true"> 
    </activity> 

通知码: 改变了的PendingIntent是如何产生的。

mNotificationManager = (NotificationManager) 
     this.getSystemService(Context.NOTIFICATION_SERVICE); 
Intent resultIntent = new Intent(this, AcceptRejectActivity.class); 
resultIntent.putExtra("message","Do you choose to accept this message?");  

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

String msg = "You have a message" 
NotificationCompat.Builder mBuilder = 
     new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_logo) 
       .setContentTitle("Message") 
       .setStyle(new NotificationCompat.BigTextStyle() 
         .bigText(msg)) 
       .setContentText(msg) 
       .setPriority(Notification.PRIORITY_HIGH); 
mBuilder.setContentIntent(contentIntent); 
mBuilder.setAutoCancel(true); 

android.app.Notification note = mBuilder.build(); 
note.defaults |= android.app.Notification.DEFAULT_VIBRATE; 
note.defaults |= android.app.Notification.DEFAULT_SOUND;   

mNotificationManager.notify(0, note); 

现在,当我做我的处理我就可以调用finish()或getActivity()。结束(),它会带我回任何应用程序,你是在点击通知前/浏览器页面。

0

好that'sa有点怪异,但就像一个选项,可以在完成您的应用程序后打开浏览器。

private void allDone() { 
     Log.i(TAG, "The choice has been made");    
     getActivity().finish(); //finish your app!. 
     //NavUtils.navigateUpFromSameTask(getActivity()); 

     try { //Open browser!. 
      Uri uri = Uri.parse("googlechrome://navigate?url="); 
      Intent i = new Intent(Intent.ACTION_VIEW, uri); 
      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(i); 
     } catch (ActivityNotFoundException e) { 
      // Chrome is not installed 
     } 

} 

然后您将返回到Chrome会话;)。

+0

是的,这有点奇怪。调用完成()使用关闭我的应用程序,并返回到我的任何应用程序。有点像跨应用onbackpressed。然后有一天它停止工作。 这次重新启动chrome,但没有使我回到当前页面。如果我在脸书,收件箱或任何其他应用程序中仍然无法解决问题。 – ChoklatStu

+0

这会告诉你最后一个打开的页面在chrome中,如果你想打开一个特定的页面,只需添加url,例如:?url = http://www.facebook.com – Jorgesys

相关问题