Mag*_*ius 74 notifications android android-intent
我写了一个简单的Android应用程序,显示如下自定义通知:
Context context = getApplicationContext();
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification( R.drawable.icon, title, System.currentTimeMillis());
Intent notificationIntent = new Intent( context, this.getClass());
notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE");
PendingIntent pendingIntent = PendingIntent.getActivity( context , 0, notificationIntent, 0);
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(context.getPackageName(), R.layout.notifypbar);
notification.contentIntent = pendingIntent;
notification.contentView.setTextViewText(R.id.notifypb_status_text, text);
notification.contentView.setProgressBar(R.id.notifypb_status_progress, 100, (int)(100*progress), false);
manager.notify(104, notification);
Run Code Online (Sandbox Code Playgroud)
这段代码在我的应用程序中被称为ONLY ONCE,它显示带有进度条的通知(全部正确).
现在,当用户点击此通知时,我的应用程序会处理该onResume
事件.
public void onResume()
{
super.onResume();
// TODO: Extras è SEMPRE NULL!!! impossibile!
Intent callingintent = getIntent();
Bundle extras = callingintent.getExtras();
Run Code Online (Sandbox Code Playgroud)
但额外的东西总是空的!
我尝试过任何组合:
notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE");
Run Code Online (Sandbox Code Playgroud)
要么
Bundle extra = new Bundle();
extra.putString(key, value);
notificationIntent.putExtra(extra);
Run Code Online (Sandbox Code Playgroud)
但是getIntent().getExtras()总是返回NULL.
Mag*_*ius 113
这是场景:
该方法getIntent()
返回FIRST意图而不是启动活动.
因此,当活动被CLOSED(终止)并且用户点击通知时,它将运行活动的新实例getIntent()
并按预期工作(Extras 不是 null
).
但是,如果活动"正在休眠"(它在后台)并且用户单击通知,则getIntent()
始终返回启动活动的第一个意图而不是通知意图.
因此,要在应用程序运行时捕获通知意图,只需使用它即可
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
然后重写onNewIntent(Intent newintent)
.
因此,当应用程序首次运行时,getIntent()
可以使用,当应用程序从睡眠状态恢复时,onNewIntent
可以正常工作.
coo*_*994 90
只需将此代码写在您的Resume()方法之上.这就是全部.这会刷新意图 - 我真的不知道,但它确实有效.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
Run Code Online (Sandbox Code Playgroud)
Muh*_*hir 15
问题:您正在为待处理的强制发送相同的请求代码.改变这个.
解决方案:设置全局变量int UNIQUE_INT_PER_CALL = 0以及创建pendingIntent调用时如下所示.
PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);
UNIQUE_INT_PER_CALL++; // to increment.
Run Code Online (Sandbox Code Playgroud)
UNIQUE_INT_PER_CALL ++; //增加
归档时间: |
|
查看次数: |
39122 次 |
最近记录: |