我正在开发一个应用程序,我只需要一个带广播接收器的外部应用程序。这是我的代码:
APP1:
Intent intent = new Intent();
intent.setAction("com.blabla.myaction");
intent.putExtra("extra", "test");
sendBroadcast(intent);
app2(带接收器的那个):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<receiver
android:name=".myReceiver">
<intent-filter>
<action android:name="com.blabla.myaction" />
</intent-filter>
</receiver>
</manifest>
public class myReceiver extends BroadcastReceiver {
private Context mContext;
public static final String ACTION = "com.blabla.myaction";
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
if (ACTION.equals(intent.getAction())) {
Log.e("lala", "received");
String extra = intent.getStringExtra("extra");
if (packageName != null) {
Log.e("lala", extra);
}
}
}
有了这个,我没有得到“收到”的日志,也没有得到额外的。为什么呢?
答案 0 :(得分:1)
您“接收申请必须至少启动一次。
您可能需要查看https://developer.android.com/reference/android/content/BroadcastReceiver.html。