onHandleIntent 没有被调用

onHandleIntent is not getting called

在我正在开发的应用程序中,我正在尝试使用 IntentService,如下面的代码所示。 IntentService 在清单文件中的声明如下所示。 我现在面临的问题是,当我 运行 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(),或链接到超类的 onStartCommand() 实现。现在,您正在覆盖内置实现,IntentService 使用它来设置后台线程并调用 onHandleIntent().