待意图演员活动时暂停只收到 [英] Pending intent extras are received only when activity is paused
问题描述
我是从我的 StateCh.java
发送额外的字符串挂起的意图 MainActivity
。我在上面的期望是未决的意图额外的抵(通知点击)时显示在 MainActivity
对话框。问题是,当我打开 MainActivity
然后我点击该通知有一个未决的意图和对话框内没有额外不显示。当我暂停 MainActivity
(由pressing后退键),然后再次单击通知其按预期工作。
MainActivity.java:
公共类MainActivity延伸活动{ // ... @覆盖
保护无效onNewIntent(意向意图){
super.onNewIntent(意向); 捆绑额外= getIntent()getExtras()。
如果(临时演员!= NULL){
字符串值1 = extras.getString(信息);
Log.v(警报,值1); AlertDialog alertDialog =新AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle(称号);
alertDialog.setMessage(值);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,OK,新DialogInterface.OnClickListener(){ 公共无效的onClick(DialogInterface对话,诠释它){ // startActivity(MainActivity.this.getIntent()); }
}); alertDialog.show();
}
}
}
StateCh.java:
公共类StateCh延伸服务{// ... 私人无效notificationU(标题字符串,字符串文本){ //当用户点击展开的通知的意图发动
意向意图=新意图(这一点,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(信息,东西);
intent.setAction(actionstring+ System.currentTimeMillis的()); 的PendingIntent pendIntent = PendingIntent.getActivity(这一点,0,意向,PendingIntent.FLAG_UPDATE_CURRENT); 通知noti2 =新NotificationCompat.Builder(本)
.setContentTitle(职称)
.setContentText(文本)
.setSmallIcon(R.drawable.warning)
.setContentIntent(pendIntent)
。建立(); mNotificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(123456,noti2);
} // ...}
修改捆绑额外= getIntent()getExtras();
要捆绑额外= intent.getExtras();
,或致电<一个href=\"http://developer.android.com/reference/android/app/Activity.html#setIntent%28android.content.Intent%29\"相对=nofollow> setIntent(意向)第一个
I'm sending extra string with pending intent from my StateCh.java
to MainActivity
. My expectation on it is to display dialog in MainActivity
when pending intent with extra is arrived (notification is clicked). The problem is when i open the MainActivity
and then I click the notification there is no extras inside pending intent and dialog isn't displayed. When I pause the MainActivity
(by pressing back button) and click the notification again it works as expected.
MainActivity.java:
public class MainActivity extends Activity {
//...
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String value1 = extras.getString("message");
Log.v("alert", value1);
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("title");
alertDialog.setMessage(value1);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//startActivity(MainActivity.this.getIntent());
}
});
alertDialog.show();
}
}
}
StateCh.java:
public class StateCh extends Service {
//...
private void notificationU(String title, String text) {
//The intent to launch when the user clicks the expanded notification
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("message", "something");
intent.setAction("actionstring" + System.currentTimeMillis());
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification noti2 = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.warning)
.setContentIntent(pendIntent)
.build();
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(123456, noti2);
}
// ...
}
Change Bundle extras = getIntent().getExtras();
To Bundle extras = intent.getExtras();
or call setIntent(intent) first
这篇关于待意图演员活动时暂停只收到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!