gpt4 book ai didi

android - onHandleIntent 没有被调用

转载 作者:行者123 更新时间:2023-11-29 23:39:07 26 4
gpt4 key购买 nike

在我正在开发的应用程序中,我正在尝试使用 IntentService,如下面的代码所示。 IntentService 在 list 文件中的声明如下所示。我现在面临的问题是,当我运行 App 时,永远不会调用 onHandleIntent。

我检查了一些内部示例,但没有一个有用,因为解决问题的建议提示不起作用。

我启动服务如下:

this.mButtonFetchURL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mServiceIntent = new Intent(getApplicationContext(), TwitterTrendsAPIService.class);
mServiceIntent.putExtra(CONST_KEY_REQUEST_URL, BASE_REQUEST_URL);
startService(mServiceIntent);
clearEditText(mEditTextURLContents);
}
});

请告诉我如何解决。

代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pc_.twittertrendsnearlocation">

<uses-permission android:name="android.permission.INTERNET" />

<application
....
....
....
<service
android:name=".services.TwitterTrendsAPIService"
android:exported="false"
android:enabled="true"/>
</application>

代码

public class TwitterTrendsAPIService extends IntentService {

private static final String TAG = TwitterTrendsAPIService.class.getSimpleName();
private boolean mIsFetching = false;

private String mBaseRequestURL = null;
private RequestQueue mRequestQueue = null;
private JsonArrayRequest mJsonArrayRequest = null;

private final static String CONST_KEY_JSON_ARRAY_TRENDS = "trends";
private JSONObject mEntireJSONObject = null;
private JSONArray mEntireTrendsArray = null;

public TwitterTrendsAPIService() {
super(null);
}

/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public TwitterTrendsAPIService(String name) {
super(name);
}

@Override
public void onCreate() {
super.onCreate();
Log.w(TAG, "[onCreate]");

this.setupVolley();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w(TAG, "[onStartCommand]");

return Service.START_NOT_STICKY;
}

@Override
public void onHandleIntent(Intent intent) {
Log.w(TAG, "[onHandleIntent]");
}

@Override
public void onDestroy() {
super.onDestroy();
Log.w(TAG, "[onDestroy]");
}

private void setupVolley() {
this.mRequestQueue = Volley.newRequestQueue(this);
}

private class ServiceRunnable implements Runnable {

@Override
public void run() {
fetchJSONData();
stopSelf();
}
}

private void fetchJSONData() {
Log.w(TAG, "@fetchJSONData");

this.mJsonArrayRequest = new JsonArrayRequest(Request.Method.GET, this.mBaseRequestURL, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
mEntireJSONObject = response.getJSONObject(0);
mEntireTrendsArray = mEntireJSONObject.getJSONArray(TwitterTrendsAPIService.CONST_KEY_JSON_ARRAY_TRENDS);
Log.i(TAG, "mEntireTrendsArray.length(): " + mEntireTrendsArray.length());
Log.i(TAG, "mEntireTrendsArray.get(0): " + mEntireTrendsArray.get(0));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});
}
}

最佳答案

删除 onStartCommand(),或链接到父类(super class)的 onStartCommand() 实现。现在,您正在覆盖内置实现,IntentService 使用它来设置后台线程并调用 onHandleIntent()

关于android - onHandleIntent 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52018790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com