在工作中,接收其它应用程序数据时,静态注册了广播,可是却接收不到消息,很是郁闷,经过看书,查阅资料,最终解决了问题,问题出在action的配置上(这个action的配置如果是低于4级是接收不到广播的,这里是个坑 com.x.x.x 。之前配置3级,就怎么都接收不到广播)。
需求是:两个应用程序工程名称分别是:Demo2_broadcastReceiver,Demo1_intent 。
Demo2_broadcastReceiver这个工程通过发广播的方式给工程Demo1_intent传递数据,
Demo1_intent工程接收到数据之后,返回一些数据给工程Demo2_broadcastReceiver 。
Demo2_broadcastReceiver工程代码如下:
package com.ymt.demo2_broadcastreceiver;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
/**
* 调用者
*
* @author user
*点击按钮时发送广播给Demo1_intent工程
*/
public class MainActivity extends Activity {
private String datas="你好,我是调用者,mainApp.会返回给你数据,请查收!!!";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
//intent后面的参数是和接收者manifest.xml配置文件中广播的<intent-filter>中的<action/>相匹配的
Intent broadcast = new Intent("com.ybs.send.demo_ybs_broadcastreceiver");
broadcast.putExtra("mainApp_send_data", datas);
sendBroadcast(broadcast);
}
}
package com.ymt.demo2_broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
//接收到被调用者返回的数据
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("tag", "otherApp广播接收者,这是调用者,接收ybs返回的数据!!!");
if("com.ybs.return.data.action".equals(intent.getAction())){
Log.i("tag", "接收到ybs返回数据的广播!!!action=="+intent.getAction());
String data=intent.getStringExtra("return_data");
Log.i("tag", "这是ybs返回给调用者的数据=="+data);
}
}
}
Android manifest.xml配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ymt.demo2_broadcastreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<!-- 这个action的配置如果是低于4级是接收不到广播的,这里是个坑 com.x.x.x 之前配置3级,就怎么都接收不到广播-->
<action android:name="com.ybs.return.data.action"/>
</intent-filter>
</receiver>
</application>
</manifest>
Demo1_intent工程代码如下:
package com.ybs.ymt;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
private String return_caller_data;
private String parser_caller_data;
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("tag", "接收到调用者的数据");
if("com.ybs.send.demo_ybs_broadcastreceiver".equals(intent.getAction())){
Log.i("tag", "接收到调用者的action=="+intent.getAction());
String mainData=intent.getStringExtra("mainApp_send_data");
Log.i("tag", "接收到调用者的data=="+ parserCallerData(mainData));
//接收到调用者的广播,回复调用者数据
Intent return_data_intent=new Intent("com.ybs.return.data.action");
return_data_intent.putExtra("return_data", returnCallerData());
context.sendBroadcast(return_data_intent);
}
}
//解析调用者传过来的数据
private String parserCallerData(String param){
parser_caller_data=param;
return parser_caller_data;
}
//返回给调用者数据。
private String returnCallerData(){
return_caller_data="我是被调用者,返回给调用者数据,ybsybsybs。";
return return_caller_data;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ybs.ymt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true" >
<!-- 这个action的配置如果是低于4级是接收不到广播的,这里是个坑 com.x.x.x 之前配置3级,就怎么都接收不到广播-->
<intent-filter>
<action android:name="com.ybs.send.demo_ybs_broadcastreceiver"></action>
</intent-filter>
</receiver>
</application>
</manifest>