通过IntentService创建的通知使用传真错误意向 [英] Notification created by IntentService uses allways a wrong Intent
问题描述
当用户presses 发送按钮1骨节病>(向下滚动到看到应用程序的构建)一个新的<一个href="http://developer.android.com/reference/android/app/Notification.html"><$c$c>Notification$c$c>从 RefreshService
创建。如果用户presses此通知的 MainActivity
实例被启动,并且收到的 字符串
的值按钮1
过<一href="http://developer.android.com/reference/android/content/Intent.html"><$c$c>Intent$c$c>.
When the user presses Send "Button 1"(scroll down to see the construction of the app) a new Notification
is created from the RefreshService
. If the user presses this notification a MainActivity
instance gets started and receives a String
with the value Button 1
over the Intent
.
此值将被显示出来。
当用户现在presses的发送按钮2骨节病>新的<一个href="http://developer.android.com/reference/android/app/Notification.html"><$c$c>Notification$c$c>从 RefreshService
创建。如果用户presses此通知的 MainActivity
实例被启动,并且收到的 字符串
同时的值按钮1
在<一个href="http://developer.android.com/reference/android/content/Intent.html"><$c$c>Intent$c$c>.
When the user presses now the Send "Button 2" a new Notification
is created from the RefreshService
. If the user presses this notification a MainActivity
instance gets started and receives a String
ALSO with the value Button 1
over the Intent
.
因此,大家可以猜到,通常应该有值按钮2
。
So as you can guess, normally there should be the value Button 2
.
在第一个按钮,用户pressed WAS 发送按钮2骨节病>那么就八方通按钮2
发送。
When the first Button the user pressed was Send "Button 2" then there would allways Button 2
be sent.
唯一sollution获得第二个按钮的值是重启手机和$ P $第一pssing第二个按钮。即使是强制关闭不工作。
The only sollution to get the value of the second button is to restart the phone and pressing the second button first. Even force close doesn't work.
我知道,我也可以改变用户界面以另一种方式。但我需要这种方法的应用程序,我需要重新启动与另一 意图
这样的做法应该是相同的。
I know that I also can change the UI in another way. But I need this approach in a app where I need to restart the 'MainActivity' with another Intent
so the approach should be the same.
-
一个
活动
名为MainActivity
A <一个href="http://developer.android.com/reference/android/app/IntentService.html"><$c$c>IntentService$c$c>名为 RefreshService
MainActivity 的
MainActivity
public class MainActivity extends Activity implements View.OnClickListener {
public static final String RECEIVED = "received";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((TextView)findViewById(R.id.textView_received)).setText(getIntent().getStringExtra(RECEIVED));
findViewById(R.id.button_1).setOnClickListener(this);
findViewById(R.id.button_2).setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, RefreshService.class);
if(v.getId() == R.id.button_1){
intent.putExtra(RECEIVED, "Button 1");
Toast.makeText(this,"Sent \"Button 1\"",Toast.LENGTH_LONG).show();
}
else if(v.getId() == R.id.button_2){
intent.putExtra(RECEIVED, "Button 2");
Toast.makeText(this,"Sent \"Button 2\"",Toast.LENGTH_LONG).show();
}
startService(intent);
}
}
RefreshService 的
RefreshService
public class RefreshService extends IntentService {
public RefreshService() {
super("RefreshService");
}
@Override
protected void onHandleIntent(Intent intent) {
String received = intent.getStringExtra(MainActivity.RECEIVED);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra(MainActivity.RECEIVED, received);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContentTitle("IntentServiceRefresh").setContentText(received).setSmallIcon(R.drawable.ic_notification_small).setContentIntent(pendingIntent);
Notification notification = builder.build();
// Hide the notification after it's selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
应用布局的
App Layout
要简单地测试了这一点,我创建了一个应用程序为Android的工作室项目,该项目 可以在这里找到:<一href="https://github.com/SimonMayerhofer/IntentServiceRefresh">https://github.com/SimonMayerhofer/IntentServiceRefresh
To simply test this I created a app as a Android Studio Project which can be found here: https://github.com/SimonMayerhofer/IntentServiceRefresh
推荐答案
我的怀疑是,因为唯一的意图改变的是群众演员,在 PendingIntent.getActivity(...)
工厂方法是简单地重新使用旧的意图作为优化。
My suspicion is that, since the only thing changing in the Intent is the extras, the PendingIntent.getActivity(...)
factory method is simply re-using the old intent as an optimization.
在RefreshService,请尝试:
In RefreshService, try:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
请参阅:
<一个href="http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT">http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT
这篇关于通过IntentService创建的通知使用传真错误意向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!