码迷,mamicode.com
首页 > 移动开发 > 详细

Android 自定义广播发送和接收

时间:2014-08-27 23:21:28      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   os   java   使用   io   

android系统会发送许多系统级别的广播,比如屏幕关闭,电池电量低等广播。同样应用可以发起自定义“由开发者定义的”广播。广播是从一个应用内部向另一个应用发送消息的途径之一。

BroadcastReceiver是一个可以监听和响应广播的组件。本文中,我们将会演示如何发送自定义广播以及如何通过编程和使用Manifest文件定义一个BroadcastReceiver来监听这一广播。我们最后只要调用sendBroadcast就可以发送广播信息了。

 

bubuko.com,布布扣

 

 

1,编写MyReceiver,MyReceiver代码主要是继承BroadcastReceiver的接收类。

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.EditText;

public class MyReceiver extends BroadcastReceiver {
    
    private final static String TAG = "BR";
    
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "broadcast: " + intent.getAction() + "\n");
    }
}

 

2,注册广播,可以通过java代码动态注册或在Manifest xml文件中进行注册。

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.guangbo.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="com.example.guangbo.MyReceiver" >
             <intent-filter>
                 <action android:name="com.example.android.USER_ACTION" />
             </intent-filter>
         </receiver>
    </application>

</manifest>

 

3,发送,可以调用activity对象的方法sendBroadcast就可以发送广播了。

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

public class MainActivity extends Activity implements OnClickListener {

    Button btn = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) this.findViewById(R.id.btnSendBroadcast);
        btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        Intent i = new Intent("com.example.android.USER_ACTION");
        sendBroadcast(i);
    }
}

 

我也是学习别人的,感谢百度,感谢下面文章的博主。

http://blog.csdn.net/zajin/article/details/12992705

 

Android 自定义广播发送和接收

标签:android   style   blog   http   color   os   java   使用   io   

原文地址:http://www.cnblogs.com/chenjianxiang/p/3940514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!