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>
效果:
(总结:广播接受者分静态和动态广播接受者,静态是在列表清单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>
效果:
相关文章推荐
- andriod之BroadcastReciever--广播接受者,启动acitivity,和service,以及停止service
- Android开发之广播接受者Broadcast…
- Android基础进阶之Broadcast(广播接受者)的学习和研究
- Android 广播BroadcastReceiver基础----重新认识Android(13)
- 通过广播BroadcastReciever使Andro…
- Android-广播BroadcastReceiver-自定义广播接受者和发送者
- 4.1.3.3 Android 网络状态监听的静态广播接受者和动态广播接受者Broadcast-Receiver
- 广播接受者(Broadcast Receiver)
- 使用广播接受者BroadcastReceiver监测网络变化
- Android之BroadcastReceiver(广播接受者)详解
- 基础篇(五) BroadcastReciever(广播接收器)和Broadcast(广播)
- BroadcastReciever广播接收者
- Android广播接受者BroadcastReceiver详解
- Android之BroadcastReceiver 广播接受者(短信窃听实例)
- Android中启动一个Service和广播接受者BroadcastReceiver,用于随时观察网络情况
- Android之BroadcastReceiver 广播接受者(短信窃听实例)
- Android之Broadcast, BroadcastReceiver(广播)
- Intent,广播Broadcast,和message数据交替
- BroadcastReceiver Android的广播解析
- android4大组件之BroadcastReceiver广播接收者