我的自定义广播接收器没有接收到意图

时间:2018-12-07 20:19:58

标签: android broadcastreceiver

我编写了自定义广播接收器来接收来自其他应用程序的意图,但是它什么也没收到。我确定第一个应用程序正在发送广播。有人能帮我吗? 应用1:

urllib3

然后我在setOnClickListener方法上运行此函数。 有我的广播接收器:

public void broadcastIntent() {
    Intent intent = new Intent();
    String permissions = "com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION";
    intent.putExtra("name", editName.getText().toString());
    intent.putExtra("price", Float.parseFloat(editPrice.getText().toString()));
    intent.putExtra("quantity", Integer.parseInt(editQuantity.getText().toString()));
    intent.setAction("com.example.android.projekt1.notification");
    sendBroadcast(intent, permissions);
}

} 因此,在发送广播时,我收到0祝词,并且我的服务也无法运行。 接收者有androidManifest:

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Intennt received.", Toast.LENGTH_LONG).show();
    Intent serviceIntent = new Intent(context, MyService.class);
    serviceIntent.putExtras(intent);
    context.startService(serviceIntent);
}

<?xml version="1.0" encoding="utf-8"?>

我先运行接收器,然后运行主应用程序,然后按按钮发送广播

1 个答案:

答案 0 :(得分:0)

设置定位应用程序的程序包名称:

public void broadcastIntent() {
    Intent intent = new Intent();
    String permissions = "com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION";
    intent.putExtra("name", editName.getText().toString());
    intent.putExtra("price", Float.parseFloat(editPrice.getText().toString()));
    intent.putExtra("quantity", Integer.parseInt(editQuantity.getText().toString()));
    intent.setAction("com.example.android.projekt1.notification");
    intent.setPackage("Package name of receiver app");// set Package of targeting app
    sendBroadcast(intent, permissions);
}