getIntent()额外总是空 [英] getIntent() Extras always NULL

查看:171
本文介绍了getIntent()额外总是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的Andr​​oid应用程序,显示像这样的自定义通知:

 上下文语境= getApplicationContext();
NotificationManager经理=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
通知通知=新的通知(R.drawable.icon,标题,System.currentTimeMillis的());
意图notificationIntent =新的意图(背景下,this.getClass());
notificationIntent.putExtra(com.mysecure.lastpage,SECURE code);
PendingIntent pendingIntent = PendingIntent.getActivity(上下文,0,notificationIntent,0);
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView =新RemoteViews(context.getPackageName(),R.layout.notifypbar);
notification.contentIntent = pendingIntent;

notification.contentView.setTextViewText(R.id.notifypb_status_text,文本);
notification.contentView.setProgressBar(R.id.notifypb_status_progress,100,(INT)(100 *进度),FALSE);

manager.notify(104,通知);
 

这一块code只调用一次在我的应用程序,它会显示一个进度条的通知(全部正常)。

现在,当用户点击该通知我的应用程序处理的 onResume 事件。

 公共无效onResume()
{
    super.onResume();
    // TODO:额外èSEM pre空! impossibile!
    意图callingintent = getIntent();
    捆绑额外= callingintent.getExtras();
 

但演员总是NULL!

我试过的任意组合:

  notificationIntent.putExtra(com.mysecure.lastpage,SECURE code);
 

 捆绑额外=新包();
extra.putString(键,值);
notificationIntent.putExtra(额外);
 

但getIntent()。getExtras()返回总是NULL。

解决方案

这是该方案:
该方法 getIntent()返回第一个意图比发射活动。

所以,当活动被关闭(终止),用户点击该通知,它将运行活动的一个新实例, getIntent()按预期工作(附加功能是的没有)。

但是,如果该活动是睡觉(这是在后台),并在用户点击该通知, getIntent()始终返回开始的第一意向活性和NOT通知意图

所以到了应用程序运行时赶通告的意图,只需使用该<​​/ P>

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

和比覆盖 onNewIntent(意向newintent)

应用程序第一次运行所以,当 getIntent()可以用来当应用程序进入休眠状态恢复时, onNewIntent 工作

I wrote a simple Android App that show a custom Notification like this:

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);

This piece of code is called ONLY ONCE in my application and it displays a notification with a progress bar (all correctly).

Now, when a user clicks on this notification my application handles the onResume event.

public void onResume()
{
    super.onResume();
    // TODO: Extras è SEMPRE NULL!!! impossibile!
    Intent callingintent = getIntent(); 
    Bundle extras = callingintent.getExtras();

but extras is always NULL!

I've tried any combination of:

notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE");

or

Bundle extra = new Bundle();
extra.putString(key, value);
notificationIntent.putExtra(extra);

but getIntent().getExtras() returns always NULL.

解决方案

This is the scenario:
The method getIntent() returns the FIRST intent than launch activity.

So, when the activity is CLOSED (terminated) and the user clicks on the notification, it will run a new instance of the activity and getIntent() works as expected (Extras is not null).

But if the activity is "sleeping" (it is in the background) and the user clicks on the notification, getIntent() always returns the very FIRST intent that started the activity and NOT the notification intent.

So to catch the notification intent while the application is running, simply use this

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

and than override onNewIntent(Intent newintent).

So when an application first runs, getIntent() can be used and when application is resumed from sleeping, onNewIntent works.

这篇关于getIntent()额外总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆