简体   繁体   中英

Android Wear Intent Extras Empty

What I am trying to do is to sent a Notification to Wear with a PendingIntent to open a simple Activity of which TextView is set by an Intents Extra (String). The problem is, that the Extras seems to be empty.

Here is my Code for the MainActivity:

public class MainActivity extends ActionBarActivity {

    public static final String KEY = "eu.paliga.creatinganotification.Key";
    public static final String MSG = "welcome back";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent viewIntent = new Intent(MainActivity.this, ViewEventActivity.class);
                viewIntent.putExtra(KEY, MSG);

                PendingIntent viewPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, viewIntent, 0);

                NotificationCompat.Builder notificationBuilder =
                        new NotificationCompat.Builder(MainActivity.this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("trying")
                        .setContentText("to figure it out")
                        .setContentIntent(viewPendingIntent);

                 NotificationManagerCompat notificationManager =
                        NotificationManagerCompat.from(MainActivity.this);

                notificationManager.notify(0, notificationBuilder.build());
            }
        });
    }
....
}

and for the Activity that is started by the Intent:

public class ViewEventActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_viewintent);
    Intent intent = getIntent();
    if (intent.hasExtra(MainActivity.KEY)) {
        String msg = intent.getStringExtra(MainActivity.KEY);
        Log.d("MyTag", msg);
        ((TextView)findViewById(R.id.textView2)).setText(msg);
    }
}
}

When you use PendingIntent.getActivity(MainActivity.this, 0, viewIntent, 0) (specifically, the 0 as the last parameter), extras do not get replaced per the PendingIntent overview :

Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals . If you use two Intent objects that are equivalent as per Intent.filterEquals , then you will get the same PendingIntent for both of them.

They go into details on how to deal with them, although the most common solution is to replace your call with:

PendingIntent.getActivity(MainActivity.this, 0, viewIntent,
    PendingIntent.FLAG_UPDATE_CURRENT)

Where FLAG_UPDATE_CURRENT denotes that the system should update the extras in the PendingIntent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM