android广播机制(上)

来源:互联网 发布:电脑优化加速软件 编辑:程序博客网 时间:2024/06/05 23:00

android的广播机制主要分为两个部分,就想我们听的广播一样,第一给部分是发送广播,第二个部分是接受广播。

代码还是很简单的。

==============TestBCActivity.java================

package com.example.testbc;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestBCActivity extends Activity {

    private Button bcButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_bc);
        bcButton = (Button) findViewById(R.id.BCButton);

        bcButton.setOnClickListener(new BCButtonListenner());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.test_bc, menu);
        return true;
    }

    class BCButtonListenner implements OnClickListener{

        @Override
        public void onClick(View arg0) {//最主要的在这里,这是发送的部分
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_EDIT);//可以认为这是发送时的类型,也就是接收的时候也是这个类型的才接受
            TestBCActivity.this.sendBroadcast(intent);//发送广播
        }
        
    }
}
====================以下是接收的====================

==========TestReceiver.java============

package com.example.testbc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class TestReceiver extends BroadcastReceiver {//首先要继承BroadcastReceiver方法

    
    public TestReceiver() {
        System.out.println("TestReceiver");
    }

    @Override
    public void onReceive(Context arg0, Intent arg1) {//在接收时,先运行构造函数,再运行onReceive,最后在onReceive运行完之后,android系统会使receiver销毁。
        System.out.println("onReceive");
    }

}
==========AndroidManifest.xml============

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testbc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testbc.TestBCActivity"
            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><!-- 决定接受什么类型的Receiver      这里就对应最前面一点代码中的“ intent.setAction(Intent.ACTION_EDIT)”这句话-->
                <action android:name="android.intent.action.EDIT" />
            </intent-filter>
        </receiver>
    </application>

</manifest>


1 0
原创粉丝点击