<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcast"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Brocast"
android:label="@string/title_activity_brocast" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<--可由manifest圖形化介面設定-->
<receiver android:name=".Myreceiver" >
<--目前只知道要用key的...-->
<intent-filter>
<action android:name="com.broadcast"/>
</intent-filter>
</receiver>
</application>
</manifest>
package com.broadcast;
import ....
public class Brocast extends Activity {
String s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brocast);
}
public void onclick(View v){
//建立一個Intent物件,並指定要廣播的訊息,PS:這個廣播訊息必需要至Manifest中註冊
<intent-filter>
<action android:name="com.broadcast"/>
</intent-filter>
Intent intent=new Intent("com.broadcast");
s="hello,this is broadcast";
intent.putExtra("key", s);
//廣播此Intent物件
sendBroadcast(intent);
}
}
<建立一個新class>
package com.broadcast;
import ....
public class Myreceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String data=intent.getStringExtra("key");
Toast.makeText(context, "值為"+data,3000).show();
}
}
沒有留言:
張貼留言