没有调用onHandleIntent

时间:2018-08-25 15:39:36

标签: android android-intent service intentservice

在我正在开发的App中,我正尝试使用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) {

        }
    });
}
}

1 个答案:

答案 0 :(得分:1)

删除Interface,或链接到onStartCommand()的超类实现。现在,您正在重写内置实现,onStartCommand()使用它来设置后台线程并调用IntentService