标签:
1 package com.example.metrox.broadcastreceiver1; 2 3 import android.content.Intent; 4 import android.content.IntentFilter; 5 import android.provider.Settings; 6 import android.support.v7.app.AppCompatActivity; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.EditText; 10 11 public class MainActivity extends AppCompatActivity { 12 EditText et; 13 private MyReceiver myReceiver = null; 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 et = (EditText) findViewById(R.id.editText); 19 findViewById(R.id.btnSendMessage).setOnClickListener(new View.OnClickListener() { 20 @Override 21 public void onClick(View view) { 22 // Intent intent = new Intent(MainActivity.this,MyReceiver.class); 23 Intent intent = new Intent(MyReceiver.ACTION); 24 intent.putExtra("data","这是发送到广播接收器的信息" + et.getText().toString() ); 25 //sendBroadcast(intent); 26 sendOrderedBroadcast(intent,null); 27 } 28 }); 29 30 findViewById(R.id.btnRegReciver).setOnClickListener(new View.OnClickListener() { 31 @Override 32 public void onClick(View view) { 33 if(myReceiver == null){ 34 myReceiver = new MyReceiver(); 35 registerReceiver(myReceiver,new IntentFilter(myReceiver.ACTION)); 36 System.out.println("Register"); 37 } 38 } 39 }); 40 41 findViewById(R.id.btnUnRegReciver).setOnClickListener(new View.OnClickListener() { 42 @Override 43 public void onClick(View view) { 44 if(myReceiver != null){ 45 unregisterReceiver(myReceiver); 46 System.out.println("UnRegister"); 47 myReceiver = null; 48 } 49 } 50 }); 51 } 52 }
1 package com.example.metrox.broadcastreceiver1; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 7 public class MyReceiver extends BroadcastReceiver { 8 public static final String ACTION = "com.example.metrox.broadcastreceiver1.intent.action.MyReceiver"; 9 public MyReceiver() { 10 } 11 12 @Override 13 public void onReceive(Context context, Intent intent) { 14 // System.out.println("已接收到信息,接收到的内容是" + intent.getStringExtra("data")); 15 System.out.println("MyReciver 接收到了信息"); 16 } 17 }
1 package com.example.metrox.broadcastreceiver1; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 7 public class MyReceiver1 extends BroadcastReceiver { 8 public MyReceiver1() { 9 } 10 11 @Override 12 public void onReceive(Context context, Intent intent) { 13 System.out.println("MyReciver1接收到了信息"); 14 abortBroadcast(); 15 } 16 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.metrox.broadcastreceiver1"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="动态注册广播接收器" 9 android:supportsRtl="true" 10 android:theme="@style/AppTheme"> 11 <activity android:name=".MainActivity"> 12 <intent-filter> 13 <action android:name="android.intent.action.MAIN" /> 14 15 <category android:name="android.intent.category.LAUNCHER" /> 16 </intent-filter> 17 </activity> 18 19 <receiver 20 android:name=".MyReceiver" 21 android:enabled="true" 22 android:exported="true"> 23 <intent-filter android:priority="5"> 24 <action android:name="com.example.metrox.broadcastreceiver1.intent.action.MyReceiver" /> 25 </intent-filter> 26 </receiver> 27 <receiver 28 android:name=".MyReceiver1" 29 android:enabled="true" 30 android:exported="true"> 31 <intent-filter android:priority="8"> 32 <action android:name="com.example.metrox.broadcastreceiver1.intent.action.MyReceiver" /> 33 </intent-filter> 34 </receiver> 35 </application> 36 37 </manifest>
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.metrox.broadcastreceiver1.MainActivity"> 11 12 <TextView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="Hello World!" 16 android:id="@+id/textView" /> 17 18 <EditText 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:id="@+id/editText" 22 android:layout_below="@+id/textView" 23 android:layout_alignParentLeft="true" 24 android:layout_alignParentStart="true" /> 25 26 <Button 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:text="发送信息" 30 android:id="@+id/btnSendMessage" 31 android:layout_below="@+id/editText" 32 android:layout_alignParentLeft="true" 33 android:layout_alignParentStart="true" /> 34 35 <Button 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:text="注册接收器" 39 android:id="@+id/btnRegReciver" 40 android:layout_below="@+id/btnSendMessage" 41 android:layout_alignParentLeft="true" 42 android:layout_alignParentStart="true" 43 android:layout_marginTop="60dp" /> 44 45 <Button 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" 48 android:text="注销接收器" 49 android:id="@+id/btnUnRegReciver" 50 android:layout_below="@+id/btnRegReciver" 51 android:layout_alignParentLeft="true" 52 android:layout_alignParentStart="true" 53 android:layout_marginTop="68dp" /> 54 </RelativeLayout>
标签:
原文地址:http://www.cnblogs.com/linhongquan/p/5612012.html