2016-11-10 3 views
1

通知サービスによってURLが更新されるwebviewアクティビティがあります。通知がクリックされると、毎回新しいアクティビティが作成され、変更されたURLがロードされます。これは、明示的にandroid:launchMode="singleTask"を追加することによって防ぐことができます。ウェブビューアクティビティで複数のインスタンスを防止する

通知がクリックされるたびに新しいアクティビティが作成されると、コンテンツが完全に変更されます。しかし、私は、通知がクリックされるたびに、新しいアクティビティを作成しないようにしたい(または、おそらくビューが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); 

新しい通知がクリックされるたびに、新しいアクティビティを作成しないようにしたいと考えています。

助けてください! ありがとうございます。マニフェストで :あなたの活動の

<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; 

    } 

と通知をクリックは、あなたがチェックすることができ、あなたの活動 ソリューション2を呼び出す前に

+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); 

使用このフラグを例:

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

あなたのコードを追加しました。これにより、新しい通知が届くたびに、複数のアクティビティインスタンスが起動されます。 –

+0

あなたは、アクティビティの状態を認識するための静的ブール変数を定義し、onresume()でtrueを設定し、あなたのアクティビティのonPause()メソッドでfalseを設定して、新しいアクティビティを開始する前にこの変数をチェックすることができます。 Applicationクラスから継承します。 –

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

新しいURLをリロードするには、あなたのwebviewアクティビティでメソッドonNewIntent()をオーバーライドし、新しいパラメータで何かを行います。

関連する問題