标签:
package com.example.administrator.test.Fragment.Broadcast; import android.content.Intent; import android.content.IntentFilter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.example.administrator.test.R; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); } public void bt1_OnClick(View v) { //一般广播 Intent intent=new Intent( "Broadcast.MainActivity" ); intent.putExtra( "name","广播发送测试" ); sendBroadcast( intent ); Toast.makeText( this, "一般广播已经发出去了", Toast.LENGTH_SHORT ).show(); } //动态注册 MyReceiver mr; public void bt2_OnClick(View v) { mr=new MyReceiver(); IntentFilter interFilter=new IntentFilter( "Broadcast.MainActivity1" ); // interFilter.setPriority( 1000 ); registerReceiver( mr,interFilter ); } //发送有序广播 public void bt3_OnClick(View v) { Intent intent=new Intent( "Broadcast.MainActivity" ); intent.putExtra( "name","广播发送测试" ); sendOrderedBroadcast( intent ,null);//第二个参数叫权限字符串,一般设为null Toast.makeText( this, "有序广播已经发出去了", Toast.LENGTH_SHORT ).show(); } //反注册 @Override protected void onDestroy() { if (mr!=null) { unregisterReceiver( mr ); } super.onDestroy(); } }
package com.example.administrator.test.Fragment.Broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.Toast; public class MyReceiver extends BroadcastReceiver { public MyReceiver() { Log.e( "TAG","MyReceiver被实例化" ); } @Override public void onReceive(Context context, Intent intent) { //阻断有序广播接收 if (isOrderedBroadcast()) { // abortBroadcast(); } String str=intent.getStringExtra( "name" ); intent.putExtra( "name1","这是修改的内容" ); Toast.makeText( context, "MyReceiver收到的广播="+str, Toast.LENGTH_SHORT ).show(); } }
package com.example.administrator.test.Fragment.Broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.Toast; public class MyReceiver2 extends BroadcastReceiver { public MyReceiver2() { Log.e("TAG","MyReceiver被实例化"); } @Override public void onReceive(Context context, Intent intent) { String str=intent.getStringExtra( "name1" ); Toast.makeText( context, "MyReceive2收到的广播="+str, Toast.LENGTH_SHORT ).show(); } }
<activity android:name=".Fragment.Broadcast.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <receiver android:name=".Fragment.Broadcast.MyReceiver" android:enabled="true" android:exported="true"> <intent-filter android:priority="100"> <action android:name="Broadcast.MainActivity"/> </intent-filter> </receiver> <receiver android:name=".Fragment.Broadcast.MyReceiver2" android:enabled="true" android:exported="true"> <intent-filter android:priority="100"> <action android:name="Broadcast.MainActivity"/> </intent-filter> </receiver>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送一般广播" android:onClick="bt1_OnClick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="动态注册广播接收器" android:onClick="bt2_OnClick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送有序广播" android:onClick="bt3_OnClick"/> </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/jiang2538406936/p/5730074.html