将通知中的数据发送到 activity 并使 activity 相应地运行
send data from notification to activity and make activity behave accordingly
我有两种类型的通知,下载和上传通知。
点击通知会打开一个Activity,Activity有两个片段标签,一个用于下载,一个用于上传。
我想要的是,当点击上传通知时,它不仅会启动 Activity 还会打开特定的选项卡,与下载部分相同。
下面是我所做的。
// configure the intent
Intent dIntent = new Intent(this, TransferActivity.class);
dIntent.putExtra(WidgetUtils.NOTIFICATION_MESSAGE_KEY, WidgetUtils.NOTIFICATION_OPEN_DOWNLOAD_TAB); // open download task tab
dIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent dPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, dIntent, PendingIntent.FLAG_UPDATE_CURRENT);
downloadNotification = new Notification(R.drawable.notification_bar_downloading, getString(R.string.notification_bar_title_download_started), System.currentTimeMillis());
downloadNotification.flags |= Notification.FLAG_AUTO_CANCEL;
downloadNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_bar_progress_layout);
downloadNotification.contentIntent = dPendingIntent;
Intent uIntent = new Intent(this, TransferActivity.class);
uIntent.putExtra(WidgetUtils.NOTIFICATION_MESSAGE_KEY, WidgetUtils.NOTIFICATION_OPEN_UPLOAD_TAB); // open upload task tab
uIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent uPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, uIntent, PendingIntent.FLAG_UPDATE_CURRENT);
uploadNotification = new Notification(R.drawable.notification_bar_uploading, getString(R.string.notification_bar_title_upload_started), System.currentTimeMillis());
uploadNotification.flags |= Notification.FLAG_AUTO_CANCEL;
uploadNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_bar_progress_layout);
uploadNotification.contentIntent = uPendingIntent;
notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
我是这样处理Activity中传递过来的数据的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
if (extras.containsKey(WidgetUtils.NOTIFICATION_MESSAGE_KEY)) {
// extract the extra-data in the Notification
String msg = extras.getString(WidgetUtils.NOTIFICATION_MESSAGE_KEY);
if (msg.equals(WidgetUtils.NOTIFICATION_OPEN_DOWNLOAD_TAB)) {
currentPosition = 0;
indicator.setCurrentItem(0);
} else if (msg.equals(WidgetUtils.NOTIFICATION_OPEN_UPLOAD_TAB)) {
currentPosition = 1;
indicator.setCurrentItem(1);
}
}
}
}
但问题是无论我单击哪种类型的通知,它总是会启动“上传”选项卡。这真是令人困惑。
任何人都可以帮忙吗?
提前致谢。
根据 http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent) 上的文档,您的 onNewIntent 可能不会被调用。
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.
So, if your activity is a normal one, you should use the intent from getIntent(), in onCreate() or onResume().
此外,要处理这两种情况,您可以考虑在 onNewIntent() 中调用 setIntent() 并按照文档中的建议在 onResume() 中使用 getIntent。
我最近遇到了类似的问题。经过几次调查,我发现 onNewIntent
在同一个 Intent
上被调用。我最终使用了一种非常适合我的解决方法。我会和你分享。
- 不是通过
Intent
调用 TransferActivity
,而是引入一个中间人 NotificationChannelActivity
。所有对通知的点击都将被引导到这个中间人。
- 提取中间人
Intent
传递的数据,传递给TransferActivity
。您可以使用 getIntent
API 提取数据,然后将其重新打包到一个新的 Intent
中,然后在 TransferActivity
开始实际处理数据。
在 `PendingIntent 中使用唯一标识符:
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent.getActivity(getApplicationContext(), iUniqueId, uIntent, PendingIntent.FLAG_UPDATE_CURRENT);
我有两种类型的通知,下载和上传通知。 点击通知会打开一个Activity,Activity有两个片段标签,一个用于下载,一个用于上传。 我想要的是,当点击上传通知时,它不仅会启动 Activity 还会打开特定的选项卡,与下载部分相同。
下面是我所做的。
// configure the intent
Intent dIntent = new Intent(this, TransferActivity.class);
dIntent.putExtra(WidgetUtils.NOTIFICATION_MESSAGE_KEY, WidgetUtils.NOTIFICATION_OPEN_DOWNLOAD_TAB); // open download task tab
dIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent dPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, dIntent, PendingIntent.FLAG_UPDATE_CURRENT);
downloadNotification = new Notification(R.drawable.notification_bar_downloading, getString(R.string.notification_bar_title_download_started), System.currentTimeMillis());
downloadNotification.flags |= Notification.FLAG_AUTO_CANCEL;
downloadNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_bar_progress_layout);
downloadNotification.contentIntent = dPendingIntent;
Intent uIntent = new Intent(this, TransferActivity.class);
uIntent.putExtra(WidgetUtils.NOTIFICATION_MESSAGE_KEY, WidgetUtils.NOTIFICATION_OPEN_UPLOAD_TAB); // open upload task tab
uIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent uPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, uIntent, PendingIntent.FLAG_UPDATE_CURRENT);
uploadNotification = new Notification(R.drawable.notification_bar_uploading, getString(R.string.notification_bar_title_upload_started), System.currentTimeMillis());
uploadNotification.flags |= Notification.FLAG_AUTO_CANCEL;
uploadNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_bar_progress_layout);
uploadNotification.contentIntent = uPendingIntent;
notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
我是这样处理Activity中传递过来的数据的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
if (extras.containsKey(WidgetUtils.NOTIFICATION_MESSAGE_KEY)) {
// extract the extra-data in the Notification
String msg = extras.getString(WidgetUtils.NOTIFICATION_MESSAGE_KEY);
if (msg.equals(WidgetUtils.NOTIFICATION_OPEN_DOWNLOAD_TAB)) {
currentPosition = 0;
indicator.setCurrentItem(0);
} else if (msg.equals(WidgetUtils.NOTIFICATION_OPEN_UPLOAD_TAB)) {
currentPosition = 1;
indicator.setCurrentItem(1);
}
}
}
}
但问题是无论我单击哪种类型的通知,它总是会启动“上传”选项卡。这真是令人困惑。
任何人都可以帮忙吗?
提前致谢。
根据 http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent) 上的文档,您的 onNewIntent 可能不会被调用。
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent. So, if your activity is a normal one, you should use the intent from getIntent(), in onCreate() or onResume().
此外,要处理这两种情况,您可以考虑在 onNewIntent() 中调用 setIntent() 并按照文档中的建议在 onResume() 中使用 getIntent。
我最近遇到了类似的问题。经过几次调查,我发现 onNewIntent
在同一个 Intent
上被调用。我最终使用了一种非常适合我的解决方法。我会和你分享。
- 不是通过
Intent
调用TransferActivity
,而是引入一个中间人NotificationChannelActivity
。所有对通知的点击都将被引导到这个中间人。 - 提取中间人
Intent
传递的数据,传递给TransferActivity
。您可以使用getIntent
API 提取数据,然后将其重新打包到一个新的Intent
中,然后在TransferActivity
开始实际处理数据。 在 `PendingIntent 中使用唯一标识符:
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff); PendingIntent.getActivity(getApplicationContext(), iUniqueId, uIntent, PendingIntent.FLAG_UPDATE_CURRENT);