android - onHandleIntent 没有被调用

标签 android android-intent service intentservice

在我正在开发的应用程序中,我正在尝试使用 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/

相关文章:

java - 如何从 GoogleSignInAccount 获取性别(以前的方法现已弃用)

android - 在 Android 的 TestCase 中启动服务

android - 如何测量 SpannableString 的高度?

java - 如何将第 3 方库添加到 Android AOSP 构建中?

android - 从搜索图像的内容 URI 获取绝对文件路径

android 以编程方式更新 apk 并查看安装结果

java - Android:Intent Extras 未通过 Activity 上下文之外的 startActivity() 传递

Android:将参数从Activity传递给Service

javascript - 将 Service 注入(inject) Directive AngularJS 和数据绑定(bind)中。

android - 自定义列表项不响应选择器中的 state_checked