将数据从BroadcastReceiver传递到MainActivity只能正常工作一次

时间:2011-02-06 12:05:56

标签: android android-activity android-intent task broadcastreceiver

我有PushNotificationReceiver (extends BroadcastReceiver)MainActivity。接收方通过New valueMainActivity发送一些数据(例如字符串值“Intent”)。然后MainActivity使用此值更新其TextView。 这可以正常工作,直到我将此TextView值更改为其他值(例如将其重置为“UNSPECIFIED”)并将活动再次移至背景和前景。 MainActivity已恢复,其TextView包含“New value”,但我预计它会为“UNSPECIFIED” - 这是问题

我的应用有什么问题?

整个原型项目可以下载here

这是我的MainActivity代码

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>

3 个答案:

答案 0 :(得分:1)

替换

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

通过

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

答案 1 :(得分:0)

为了解决这个问题,我添加了全局静态触发器

  /**
  * 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之前我会重置此触发器并在处理后设置(当然只有在未设置触发器时才执行处理)

答案 2 :(得分:0)

定义public static boolean newUpdate;变量,在true之前将其设置为context.startActivity(i);。然后,当您在MainActivity中查看意图包含extra时,还要验证newUpdate是否为true,如果是,请将其重置为{{ 1}}并更新您的false字段。最后一个函数看起来像这样

TextView