2016-11-10 26 views
1

我有一個webview活動,其中的url由通知服務更新。當單擊通知時,每次都會創建新的活動以加載更改的URL。這可以通過添加android:launchMode="singleTask"來避免。在webview活動中阻止多個實例

如果每次點擊通知時都會創建一個新活動,它會導致我完全更改內容。但是我想避免每次點擊通知時創建一個新的活動(或者可能創建一個視圖是idk?)。如果我在清單中添加android:launchMode="singleTask",它會給我一個副作用,即如果在現有打開的webview活動上發出通知,它不會重定向到更改的URL。

待通知的意圖:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    if (isMessage) { 
     Intent intent = new Intent(this, MiscDisplay.class); 
     intent.putExtra("start_url", url); 
     intent.setAction("NOTIFICATION_URL_ACTION"); 
     PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     mBuilder.setContentIntent(resultPendingIntent); 
     mBuilder.extend(new WearableExtender().addAction(message)); 
     mBuilder.setOngoing(false); 
     mBuilder.setSmallIcon(R.mipmap.ic_launcher); 
     mBuilder.setOnlyAlertOnce(true); 
     Notification note = mBuilder.build(); 
     mNotificationManager.notify(1, note); 

    } else { 
     mBuilder.setSmallIcon(R.mipmap.ic_launcher); 
     Intent intent = new Intent(this, MiscDisplay.class); 
     intent.putExtra("start_url", url); 
     intent.setAction("NOTIFICATION_URL_ACTION"); 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
     stackBuilder.addParentStack(MiscDisplay.class); 
     stackBuilder.addNextIntent(intent); 
     PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     mBuilder.setContentIntent(resultPendingIntent); 
     mBuilder.extend(new NotificationCompat.WearableExtender().addAction(action)); 
     mBuilder.setOngoing(false); 
     Notification note = mBuilder.build(); 
     mNotificationManager.notify(0, note); 

我想要什麼?

我想避免每次單擊新通知時創建新活動。

請幫忙! 謝謝。

+0

請粘貼掛起的意圖代碼。您正在使用它來創建通知。 –

+0

每次單擊notif時,如何更改'webview.loadUrl'處的鏈接?這是不是加載你所需的頁面? –

+0

@Akeshwar不,它不。 –

回答

1
Intent activityIntent = new Intent(context, MainActivity.class);activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
       | Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
context.startActivity(activityIntent); 

使用這個標誌調用你的活動 解決方案前兩: 在清單:

<application 
     android:allowBackup="true" 
     android:label="@string/app_name" 
     android:name=".AL" 

public class AL extends Application { 
    public static Boolean isActivityRunnig=false; 
@Override 
    public void onCreate() 
{ 
     super.onCreate(); 
} 

您的活動:

@Override 
    protected void onResume() { 
     super.onResume(); 
     AL.isActivityRunnig= true; 
    } 
@Override 
    protected void onPause() { 
     super.onPause(); 
     AL.isActivityRunnig= false; 

    } 

當點擊通知,您可以檢查falg:

if(!AL.isActivityRunnig) 
    startactivity(new Intent(context,YourActivity.class)); 
+0

我已經添加了您的代碼,這也導致在新通知到達時啓動多個活動實例。 –

+0

您可以定義一個靜態布爾變量,以瞭解活動狀態並在onresume()中設置true,並在您的activity的onPause()方法中設置false。在開始新活動之前,請檢查此變量。注意您應該定義單個音調類並從Application類擴展它。 –

0
Intent intent = new Intent(....this, ....class); 
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
intent.putExtra("...", ...); 
startActivity(intent); 

如果你想在你的WebView活動重新加載新的URL,重寫方法onNewIntent()和做新的東西PARAMS。