当应用程序被迫关闭后,不知道如何让接收者在活动中工作。即使应用程序被迫关闭,我还错过了什么来让它工作呢?任何帮助都将不胜感激。
我正在让BroadcastReceiver服务开始工作,只是在活动级别没有得到任何东西。
我有我的接收器(服务):
public class MyReceiver extends BroadcastReceiver {
public static final String SEND_NOTIFICATION_ACTION = "com.clover.sdk.app.intent.action.APP_NOTIFICATION";
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyReceiver", "Triggered MyReceiver");
String action = intent.getAction();
Bundle getIntent = intent.getExtras();
if (action.equals(SEND_NOTIFICATION_ACTION)) {
Log.i("MyReceiver Gotten", "Found");
intent = new Intent("broadCastName");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("orderId", getIntent.getString("payload"));
Log.i("Receiver OrderID", getIntent.getString("payload"));
context.sendBroadcast(intent);
}
}
}我的活动
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(broadcastReceiver, new IntentFilter("broadCastName"));
}
}然后是我在活动中的broadcastReceiver:
// Add this inside your class
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyReceiver Gotten 2", "Found");
Bundle b = intent.getExtras();
Log.i("MyReceiver Gotten 3", b.getString("orderId"));
new SpecificOrderAsyncTask(MainActivity.this).execute(b.getString("orderId"));
}
};发布于 2021-05-25 17:52:37
不知道一旦应用程序被迫关闭,如何让接收者工作。即使应用程序被迫关闭,我还错过了什么来让它工作呢?
这是矛盾的--如果承载接收器的活动被杀死,您就不能让接收方在运行时注册它的活动中工作。当你强行关闭时,应用程序过程中的每一个--包括你注册的活动和接收者--都会消失。
调用registerReceiver的目的是只在特定的时间框架或生命周期内监听广播。
如果您希望在应用程序关闭时接收方工作,不要在运行时注册它-在清单中注册它。
https://stackoverflow.com/questions/67690372
复制相似问题