Android 广播接收器

 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>
时间: 2024-11-05 06:12:40

Android 广播接收器的相关文章

Xamarin.Android广播接收器与绑定服务

一.前言 学习了前面的活动与服务后,你会发现服务对于活动而言似乎就是透明的,相反活动对于服务也是透明的,所以我们还需要一中机制能够将服务和活动之间架起一座桥梁,通过本节的学习,你将会学到广播与绑定服务,这两种方式恰恰是解决上面问题的关键. 二.简单的广播接收器 实现一个最简单的广播接收器需要继承BroadcastReceiver类,并且还要实现OnReceive方法,我们可以在项目中新建一个MainReceiver类,然后写入如下代码: 1 public class MainReceiver :

android广播接收器

Android程序创建广播接收器继承BroadcastReceiver Android广播接收器需要在AndroidManifest.xml文件中声明: <recevie android:name=".TimerBroadcastRecevier" android:enabled="true" />

Android广播接收器里弹出对话框

不多说,直接上车... 1 public class MyReceiver extends BroadcastReceiver { 2 @Override 3 public void onReceive(final Context context, Intent intent) { 4 AlertDialog.Builder builder = new AlertDialog.Builder(context); 5 builder.setTitle("提示"); 6 builder.s

Android广播接收器和Activity间传递数据

Activity向广播接收器传递数据很简单,只需要在发送广播前将数据put进Intent中就行了. 广播接收器怎么向Activity传送数据?这里要用到接口,通过在广播接收器里定义一个接口,然后让接收广播接收器数据的Activity实现这个接口.先看下面的栗子,Activity发送一个广播,然后广播接收器返回一个字符串. Activity布局文件 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearL

Android广播接收器Broadcast Receiver-android学习之旅(十二)

首先继承BroadcastReceiver类,并在manifest中注册 public class MyReceiver extends BroadcastReceiver { public MyReceiver() { } @Override public void onReceive(Context context, Intent intent) { throw new UnsupportedOperationException("Not yet implemented"); }

《Android笔记3.10》 Android 广播接收器 BroadcastReceiver

课程背景:BroadcastReceiver 是Android 四大基本组件之一,用于接收广播信息,如:开屏.锁屏.短信等等,在实际工作中用途非常广泛 核心内容:1. 动态注册和注销 BroadcastReceiver 使用 BroadcastReceiver 新建Brodercast Receiver文件: 在Receiver自动生成的onReceive方法中自定义接收到信息后的处理代码: @Override public void onReceive(Context context, Int

Android广播接收器注册问题:Caused by: java.lang.IllegalArgumentException: Receiver not registered

1.程序中明明使用如下方法进行了广播的注册和解除注册: mContext.registerReceiver(downloadReceiver, filter); mContext.unregisterReceiver(downloadReceiver); 但程序运行过程中还是有一下问题: android.app.IntentReceiverLeaked: Activity *********** has leaked IntentReceiver *********** that was ori

(八)Android广播接收器BroadcastReceiver

一.使用Broadcast Reciver 1.右击java文件夹,new->other->Broadcast Receiver后会在AndroidManifest.xml文件中生成一个receiver项 <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"

Android基础(4)——广播接收器

在Android中的每个应用程序可以对自己感兴趣的广播进行注册,这样该程序就只会接收自己所关心的广播内容,这些广播可能来自于系统的,也可能来自于其他应用程序的.Android提供了一整套完整的API,允许应用程序自由地发送和接收广播.发送广播就是借助之前了解过的Intent,接收广播则需要用到广播接收器(Broadcast Receiver).. 1.广播的类型 标准广播:Normal broadcast,是一种完全异步执行的广播,在广播发出之后,所有的广播接收器几乎都会在同一时刻接收到这条广播