您的位置:首页 > 其它

andriod之BroadcastReciever--广播接受者,之初认识

2014-12-21 14:22 375 查看
主显示布局以及代码:

(总结:广播接受者分静态和动态广播接受者,静态是在列表清单AndroidManifest.xml配置的,他不会随acitivity销毁而销毁,会一直存在,只要发送广播给它,就能接受,还可以通过intent传递数据。动态广播接受者是要先注册的,然后才能接受广播,他会随着activity的销毁而销毁)

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.day0707.MainActivity" >

<Button

android:id="@+id/sendBroadcast1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="sendBroadcast1"

/>

<Button

android:id="@+id/sendBroadcast2"

android:layout_below="@id/sendBroadcast1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="sendBroadcast2"

/>

</RelativeLayout>

Activity的java代码:

package com.example.day0707;

import android.support.v7.app.ActionBarActivity;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity {

private BroadcastReceiver mReceiver2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

registerReviceiver();

setlistener();

}

private void setlistener() {

// TODO Auto-generated method stub

findViewById(R.id.sendBroadcast1).setOnClickListener(new OnClickListener() {

@Override

public void onClick(View view) {

// TODO Auto-generated method stub

Intent intent = new Intent();

intent.putExtra("send1", "hello MyReceiver1");

intent.setAction("com.lisoft.day0707.MyReceiver1");

sendBroadcast(intent);

}

});

// TODO Auto-generated method stub

findViewById(R.id.sendBroadcast2).setOnClickListener(new OnClickListener() {

@Override

public void onClick(View view) {

// TODO Auto-generated method stub

Intent intent = new Intent();

intent.putExtra("send2", "hello MyReceiver2");

intent.setAction("com.example.day0707.MyReceiver2");//这个要和类名包名对应上

sendBroadcast(intent);

}

});

}

public void registerReviceiver(){

mReceiver2 = new MyReceiver2();

IntentFilter intentFiler = new IntentFilter();

intentFiler.addAction("com.example.day0707.MyReceiver2");//这个要和类名包名对应上

registerReceiver(mReceiver2, intentFiler);

}

class MyReceiver2 extends BroadcastReceiver{

@Override

public void onReceive(Context context, Intent intent) {

// TODO Auto-generated method stub

Log.i("main", "MyReceiver2.onReceive,接受的参数是:"+intent.getStringExtra("send2"));

}

}

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

unregisterReceiver(mReceiver2);

}

}

静态reciever代码:

package com.example.day0707;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

public class MyReceiver1 extends BroadcastReceiver{

@Override

public void onReceive(Context context, Intent intent) {

// TODO Auto-generated method stub

Log.i("main", "MyReciver1.onReceive(),接受的参数是:"+intent.getStringExtra("send1"));

}

}

清单列表:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.day0707"

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="com.example.day0707.MyReceiver1">

<intent-filter >

<action android:name="com.lisoft.day0707.MyReceiver1"/>

</intent-filter>

</receiver>

</application>

</manifest>

效果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: