0
Android/Java noob here。我在我的应用程序的另一部分中发现了一个URL。我成功地将该URL作为字符串传递给意图服务的OnStartCommand。然后我想让包含我的URL的字符串可用于驻留在OnHandleIntent中的线程中的HttpURLConnection,如下所示。我怎样才能让myUrl字符串中的URL进入OnHandleIntent?如何将字符串从OnStartCommand传递到IntentService中的OnHandleIntent中?
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String myUrl;
myUrl = intent.getExtras().getString("myUrl");
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intentNotify) {
try {
URL url = new URL("myUrl");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
readStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
re:覆盖onStartCommand这可能是noob错误。我使用了source> override/implement方法,这就是Eclipse如何插入它。至于在我尝试这个Eclipse的时候检索OnHandleIntent里面的extras说“意图不能解决”。我曾经看过一堆关于如何将字符串传递到Intent服务的讨论,并且迄今为止设法实现的唯一方法是将它们放入OnStartCommand(或OnStart,我认为现在已弃用)。 –
检查参数名称。我称它为“intent”,但在你称之为“intentNotify”的原始代码中。所以你的将是String myUrl = intentNotify.getExtras()。getString(“myUrl”); –
是的!这就是我得到的复制和粘贴我以前使用的另一个意图服务。仍然在学习,但这样的拼写错误仍然经常咬我。非常感谢您花时间回复我的问题。 –