简体   繁体   English

从服务到活动的Android intent.putExtra()

[英]Android intent.putExtra() from Service to Activity

For some time now I'm trying to pass the simple one String data from the Service to the Activity with Intent.putExtra() but with no success however. 一段时间以来,我试图将简单的一个String数据从Service传递给带有Intent.putExtra()Activity ,但是没有成功。 Intent.getStringExtra() is always NULL Intent.getStringExtra()始终为NULL

SERVICE CODE: 服务代码:

Intent intent=new Intent(getBaseContext(),MainActivity.class);
intent.putExtra(Consts.INTERNET_ERROR, "error");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(intent);

ACTIVITY CODE: 活动代码:

public void onResume() {

    super.onResume();

    Intent i = getIntent();
    String test = "temp";
    Bundle b = i.getExtras();
    if (b != null) {
        test = b.getString(Consts.INTERNET_ERROR);
    }
}   

Any suggestions? 有什么建议?

To elaborate my comment above, getIntent returns the original intent as documented at [1] http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent) [1]: 为了详细说明我的评论,getIntent返回原始意图,如[1] http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)[1 ]中所述:

The intent from the service is passed to your activity through onNewIntent which is called before onResume. 服务的意图通过onNewIntent传递给您的活动,onNewIntent在onResume之前调用。 Thus if you override onNewIntent you will get the intended string. 因此,如果重写onNewIntent,您将获得预期的字符串。

@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);

    // set the string passed from the service to the original intent
       setIntent(intent);

}

Then your code onResume will work. 然后您的代码onResume将起作用。

I don't see where you add the String in the service code, however you can run the following code to send the string. 我没有看到您在服务代码中添加String的位置,但是您可以运行以下代码来发送字符串。

Intent intent=new Intent(getBaseContext(),MainActivity.class);
Bundle b = new Bundle();
b.putString(Consts.INTERNET_ERROR, "Your String Here");
intent.putExtras(b);
intent.setAction(Consts.INTERNET_ERROR);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(intent);

First: try to move your code from onResume to onStart: 首先:尝试将代码从onResume移动到onStart:

public void onStart() {

    super.onStart();

    Intent i = getIntent();
    String test = "temp";
    Bundle b = i.getExtras();
    if (b != null) {
        test = b.getString(Consts.INTERNET_ERROR);
    }
}  

If it still not working try with this: 如果仍然无法正常运行,请尝试使用:

First activity: 第一项活动:

Intent myIntent = new Intent(firstActivity.this, secondActivity.class);
myIntent.putExtra("mystring",strValue)' <=your String
startActivity(myIntent);

Second activity: 第二项活动:

 String str = getIntent.getExtras().getString("mystring");<=get string 

and check here for some informations: 并在这里查看一些信息:

How do I pass data between Activities in Android application? 如何在Android应用程序中的活动之间传递数据?

Rather than calling 而不是打电话

extras.getString();

Try calling: 试着打电话:

intent.getStringExtra("key");

Also, are you starting the activity directly FROM the service? 另外,您是否直接服务开始活动? Or are you already running on the activity and simply want to receive data from the service? 或者您是否已经在运行活动而只是想从服务中接收数据?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM