0

私はPushNotificationReceiver (extends BroadcastReceiver)MainActivityです。受信者は、Intentを介してMainActivityに何らかのデータ(たとえば文字列値 "New value")を送信します。次にMainActivityは、TextViewをこの値で更新します。 これは、このTextViewの値を他の値に変更(たとえば、「UNSPECIFIED」にリセット)し、バックグラウンドとフォアグラウンドに再び移動するまでうまくいきます。 MainActivityが復元され、TextViewには「New value」が含まれていますが、「UNSPECIFIED」となる予定です。-これは問題です。BroadcastReceiverからMainActivityへのデータの受け渡しは、一度だけ正しく行われます

私のアプリで何が問題になっていますか?

protoプロジェクト全体をhereからダウンロードできます。ここで

は、私はグローバルな静的なトリガーを追加しました。この問題を解決するため

private TextView tvValue; 
private EditText etNewValue; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    tvValue = (TextView)findViewById(R.id.value); 
    etNewValue = (EditText)findViewById(R.id.new_value); 

    findViewById(R.id.reset).setOnClickListener(new OnClickListener() {   
     @Override 
     public void onClick(View v) { 
      tvValue.setText(getResources().getString(R.string.not_specified)); 
     } 
    }); 

    findViewById(R.id.send_notification).setOnClickListener(new OnClickListener() {   
     @Override 
     public void onClick(View v) { 
      sendNotification(etNewValue.getText().toString()); 
     } 
    }); 

    processDataFromBroadcast(getIntent());   
} 

@Override 
public void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    processDataFromBroadcast(intent); 
} 

private void sendNotification(String value){ 
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    String title = "Proto App Notif"; 

    Notification notification = new Notification(
      android.R.drawable.ic_notification_overlay, 
      title, 
      System.currentTimeMillis()); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    Context ctxApp = getApplicationContext(); 

    Intent notificationIntent = new Intent() 
     .setAction(PushNotificationReceiver.ACTION_NOTIFICATION) 
     .putExtra("value", value); 
    PendingIntent contentIntent = PendingIntent.getBroadcast(
      ctxApp, 
      0, 
      notificationIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT);  

    notification.setLatestEventInfo(
      ctxApp, 
      title, 
      value, 
      contentIntent); 

    notification.audioStreamType = AudioManager.STREAM_NOTIFICATION; 

    mNotificationManager.notify(1, notification);   
} 

private void processDataFromBroadcast(Intent intent) { 
    if (!intent.hasExtra("value")){ 
     return; 
    } 

    String val = intent.getStringExtra("value"); 

    tvValue.setText(val); // Updating my activity look 
} 

PushNotificationReceiver

private static final String LOG_CAT = "PushNotificationReceiver"; 
static final String ACTION_NOTIFICATION = "com.mobiwolf.proto.NOTIFICATION_RECEIVER"; 

@Override 
public void onReceive(Context context, Intent intent) { 
    if (!intent.getAction().equals(ACTION_NOTIFICATION)) { 
     return; 
    } 

    String value = intent.getStringExtra("value"); 

    Log.d(LOG_CAT, "Received notification message: "+value); // Log always contains the value sent on first time 

    Intent i = new Intent(); 
    i.setClass(context, MainActivity.class); 
    i.putExtra("value", value); 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    context.startActivity(i); 
} 

とマニフェスト

<receiver android:name="com.mobiwolf.proto.PushNotificationReceiver"> 
     <intent-filter> 
      <action android:name="com.mobiwolf.proto.NOTIFICATION_RECEIVER" /> 
     </intent-filter> 
    </receiver>  

    <activity android:name=".MainActivity" 
       android:label="@string/app_name" 
       android:launchMode="singleTask"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
+0

のように見えるのだろうか? –

+0

2 dave.c - はい、実際は違いは見られません。現時点では、実際には問題は活動そのものであり、放送ではありません。バックグラウンド後に復元され、インテントが再度処理され、textviewがブロードキャストからの値に設定されます。 – Solvek

答えて

0

私MainActivityコードです

/** 
    * This class triggers whether pushnotification was handled by MainActivity 
    * (Preventing handling this push notification twice when the MainActivity 
    * is moved to background and restored from background 
    */ 
    public final class PushNotifHandledTrigger { 
private static boolean handled = true; 

public static boolean wasHandled(){ 
    return handled; 
} 

public static void set(){ 
    handled = true; 
} 

public static void reset(){ 
    handled = false; 
} 
    } 

はその後startActivity前に、私はこのトリガーをリセットしないと処理した後に設定(そしてもちろんトリガーがない場合にのみ設定取り扱い行う)

1

定義

i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | 
    Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
0

によって

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

を交換してくださいpublic static boolean newUpdate;変数の場合は、context.startActivity(i);の直前にtrueに設定します。その後、あなたのMainActivityにあなたがそれと一緒にextraを持っていることを確認したら、newUpdatetrueであることを確認し、そうであればfalseにリセットし、TextViewフィールドを更新してください。最後の機能は、あなたが `PendingIntent.FLAG_CANCEL_CURRENT`の代わりに、` PendingIntent.FLAG_UPDATE_CURRENT`を使用してみましたが、この

private void processDataFromBroadcast(Intent intent) { 
    if (!intent.hasExtra("value") || (newUpdate == false)){ //So only new updates are processed. 
     return; 
    } 
    newUpdate = false; //This action will not be repeated unless another notification arrives 
    String val = intent.getStringExtra("value"); 

    tvValue.setText(val); // Updating my activity look 
} 
関連する問題