TestBroadcastA应用如下
MainActivity如下:
package cc.testbroadcasta; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 利用广播在应用程序之间传递数据 * * TestBroadcastA应用中注册一个广播接收者. * TestBroadcastB应用中发出一个广播 */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
BroadcastReceiverTest如下:
package cc.testbroadcasta; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class BroadcastReceiverTest extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String name=intent.getStringExtra("name"); int number=intent.getIntExtra("number", 0); System.out.println("接收到广播 name="+name+",number="+number); } public static void main(String[] args) { } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一个BroadcastReceiver.\n接收另外一个应用发出的广播" android:layout_centerInParent="true"/> </RelativeLayout>
AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cc.testbroadcasta" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="cc.testbroadcasta.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="cc.testbroadcasta.BroadcastReceiverTest"> <intent-filter > <action android:name="cc.test"/> </intent-filter> </receiver> </application> </manifest>
TestBroadcastB应用如下
MainActivity如下:
package cc.testbroadcastb; import android.os.Bundle; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent intent=new Intent(); //设置Action intent.setAction("cc.test"); //携带数据 intent.putExtra("name", "周星驰"); intent.putExtra("number", 9527); //发送广播 sendBroadcast(intent); } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="在这里发出一个广播\n且利用广播携带数据,传递给另外一个应用" android:layout_centerInParent="true"/> </RelativeLayout>