android开发-广播的基本用法
注册receiver有两种方法,视频里说的。一种是用xml注册,一种是直接在代码里面注册。
一, 用xml注册receiver。
注意:用xml注册的receiver并不随程序关闭而关闭。你关闭了程序,但是receiver并没有关闭,要注意。
布局:一个按钮
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="xml注册--发送广播" />
</LinearLayout>
写一个receiver类
package com.example.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
//每接收到一次广播都生成一个对象,调用onReceive之后对象销毁
//下次再接收到广播再生成一个对象。
public MyReceiver() {
System.out.println("构造方法");
}
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
System.out.println("onReceive 接收到了广播");
}
}注册receiver,写完了receiver类记住注册。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcasttest"
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.broadcasttest.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,当程序关闭之后receiver并不会关闭 -->
<receiver android:name=".MyReceiver">
<intent-filter >
<action android:name="android.intent.action.EDIT"/>
</intent-filter>
</receiver>
</application>
</manifest>
Activity类,用来发广播,测试上面写的接收器有没有接收到。
package com.example.broadcasttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_EDIT);
MainActivity.this.sendBroadcast(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}二, 用代码注册receiver。
和上面一样,写一个接收器
package com.example.broadcasttest2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class myReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
System.out.println("接收到信息了。");
}
}布局:
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="注册receiver" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="解除receiver" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>测试类
MainActivity
package com.example.broadcasttest2;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button registeReceiver;
private Button unRegistereceiver;
private Button sendBroadcast;
private myReceiver myReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//绑定receiver
registeReceiver = (Button) this.findViewById(R.id.button1);
registeReceiver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
myReceiver = new myReceiver();
IntentFilter filter = new IntentFilter();
//接收信息的action
filter.addAction(Intent.ACTION_EDIT);
MainActivity.this.registerReceiver(myReceiver, filter);
System.out.println("绑定receiver");
}
});
//解除rece
unRegistereceiver = (Button) this.findViewById(R.id.button2);
unRegistereceiver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.unregisterReceiver(myReceiver);
System.out.println("解除receiver");
}
});
//发送广播
sendBroadcast = (Button) this.findViewById(R.id.button3);
sendBroadcast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_EDIT);
MainActivity.this.sendBroadcast(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
本文详细介绍了Android开发中广播的基本概念、使用方法,并通过实例演示了如何在XML布局文件和代码中注册广播接收器。重点区分了XML注册与代码注册的区别,以及在程序关闭后接收器是否继续运行的问题。
1953

被折叠的 条评论
为什么被折叠?



