ANDROID_MARS学习笔记_S01原始版_012_广播机制一

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ANDROID_MARS学习笔记_S01原始版_012_广播机制一相关的知识,希望对你有一定的参考价值。

一、简介

技术分享

二、代码
1.xml
(1)activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7 <Button
 8     android:id="@+id/sendButton" 
 9     android:layout_width="fill_parent" 
10     android:layout_height="wrap_content" 
11     android:text="发送消息给Receiver"
12     />
13 </LinearLayout>

 

(2)AndroidManifest.xml.xml

<?xml version="1.0" encoding="utf-8"?>
<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="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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=".TestReceiver">
            <intent-filter>
                <action android:name="android.intent.action.EDIT"/>
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
</manifest>

 

2.java
(1)MainActivity.java

 1 package com.broadcast;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 
10 public class MainActivity extends Activity {
11     /** Called when the activity is first created. */
12     private Button sendButton;
13     @Override
14     public void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activity_main);
17         sendButton = (Button)findViewById(R.id.sendButton);
18         sendButton.setOnClickListener(new BroadcastListener());
19     }
20     class BroadcastListener implements OnClickListener{
21 
22         @Override
23         public void onClick(View v) {
24             Intent intent = new Intent();
25             intent.setAction(Intent.ACTION_EDIT);//要与在AndroidManifest.xml设置值一致
26             MainActivity.this.sendBroadcast(intent);
27         }
28         
29     }
30 }

 

(2)TestReceiver.java

 1 package com.broadcast;
 2 
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 
 7 public class TestReceiver extends BroadcastReceiver{
 8 
 9     public TestReceiver(){
10         System.out.println("TestReceiver");
11     }
12     @Override
13     public void onReceive(Context context, Intent intent) {
14         System.out.println("onReceive");
15     }
16 }

 

以上是关于ANDROID_MARS学习笔记_S01原始版_012_广播机制一的主要内容,如果未能解决你的问题,请参考以下文章

ANDROID_MARS学习笔记_S01原始版_005_RadioGroupCheckBoxToast

ANDROID_MARS学习笔记_S01原始版_009_下载文件

ANDROID_MARS学习笔记_S01原始版_014_WIFI

ANDROID_MARS学习笔记_S01原始版_016_Service

ANDROID_MARS学习笔记_S01原始版_006_ListView

ANDROID_MARS学习笔记_S01原始版_005_ProgressBar