我有一个BroadcastReceiver
我希望在UI线程中执行一些工作。根据{{3}},我首先调用goAsync()
,然后返回PendingResult
,然后在我的非UI线程中调用PendingResult.finish()
。
但是,我得到NullPointerException
,因为goAsync()
会返回null
。
我做错了什么?
这是一个复制行为的简单示例。我使用了targetSdkVersion 27
和minSdkVersion 23
:
package com.example.broadcastreceiverpendingintenttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PendingResult pendingResult = goAsync();
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
Log.i("tag", "performed async task");
pendingResult.finish();
return null;
}
};
asyncTask.execute();
}
}
这是一个简单的调用它的Activity:
package com.example.broadcastreceiverpendingintenttest;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public static final String ACTION = "MY_ACTION";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter(ACTION);
broadcastManager.registerReceiver(new MyReceiver(), intentFilter);
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
broadcastManager.sendBroadcast(new Intent(ACTION));
});
}
}
activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.broadcastreceiverpendingintenttest.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="232dp"
android:layout_marginEnd="148dp"
android:layout_marginStart="148dp"
android:layout_marginTop="231dp"
android:text="Press me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
我认为这里的答案是应该使用常规广播而不是本地广播:
val broadcast = Intent("my.intent.action")
broadcast.putExtra("notification", "hello")
broadcast.putExtra("payload", 4946)
activity?.sendBroadcast(broadcast)
然而,我也注意到附加调试器来读取待处理的结果值是错误的说它为空。所以在这种情况下,我也不会信任调试器。
答案 1 :(得分:-1)
我写了JobIntentService
代替:
package com.example.broadcastreceiverpendingintenttest;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.util.Log;
public class MyJobIntentService extends JobIntentService {
public static final String ACTION = "and....action!";
static final int JOB_ID = 9001;
@Override
protected void onHandleWork(@NonNull Intent intent) {
// TODO check the intent action is valid, get extras etc
Log.i("tag", "performed my task");
}
}
在AndroidManifest
中声明如下:
<service
android:name=".MyJobIntentService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false" />
MyReceiver
然后将服务的工作排入队列:
package com.example.broadcastreceiverpendingintenttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent jobIntent = new Intent();
jobIntent.setAction(MyJobIntentService.ACTION);
MyJobIntentService.enqueueWork(context, MyJobIntentService.class, MyJobIntentService.JOB_ID, jobIntent);
}
}
比我想要的更多开销,但它完成了工作。