android中广播的两种启动方式浅析【自定义Action】
来源:互联网 发布:10月经济数据 编辑:程序博客网 时间:2024/06/05 07:32
1:注册广播:(有两种方式)
一是动态方式使用Context.registerReceiver()方法来注册;
二是静态方式,即在AndroidManifest.xml中通过<receiver> 。
2:发送广播:
在需要广播的地方sendBroadcast(intent); 之前要给Intent的对象intent 设置Action (Action可以被系统的或自定义的)和 data。
3:接收广播:
就是在extends Broadcast的那个类中, onReceive(Context context, Intent intent)方法要做的事情。
***静态方式*****
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/b"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"发送"
/>
<
Button
android:id
=
"@+id/cancle"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"exit"
/>
</
LinearLayout
>
public
class
TestActivity
extends
Activity {
private
Button b;
private
Button cancle;
public
static
final
String MY_ACTION =
"mxp"
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) findViewById(R.id.b);
cancle = (Button) findViewById(R.id.cancle);
b.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
Intent intent =
new
Intent();
intent.setAction(MY_ACTION);
intent.putExtra(
"msg"
,
"asdsdas"
);
sendBroadcast(intent);
}
});
cancle.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
finish();
}
});
}
}
AndroidManifest.xml
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
package
=
"m.m.m"
android:versionCode=
"1"
android:versionName=
"1.0"
>
<uses-sdk android:minSdkVersion=
"8"
/>
<application
android:icon=
"@drawable/ic_launcher"
android:label=
"@string/app_name"
>
<activity
android:name=
".TestActivity"
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=
".BroadCastActivity"
>
<intent-filter >
<action android:name=
"mxp"
/>
</intent-filter>
</receiver>
</application>
</manifest>
总结 :自定义Action,就是定义一个静态常量如:public static final String MY_ACTION = "mxp";但是不要和系统的重复哦!
对我而言,关键是注册,即
<receiver android:name=".BroadCastActivity"> 注册广播
<intent-filter >
<action android:name="mxp"/> 定义action
</intent-filter>
</receiver>
***动态方式:*************
1)不要androidManifest.xml文件中注册 ,即删除刚才注册的部分
2) 在启动服务的地方,即发送按钮的监听器里注册,再启动服务
TestActivity.java 多了注册的代码:myReceiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(MY_ACTION);
import
android.app.Activity;
import
android.content.Intent;
import
android.content.IntentFilter;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
public
class
TestActivity
extends
Activity {
private
Button bind;
private
Button unbind;
private
Button cancle;
protected
MyReceiver myReceiver;
public
static
final
String MY_ACTION =
"mxp"
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
bind = (Button) findViewById(R.id.bind);
unbind = (Button) findViewById(R.id.unbind);
cancle = (Button) findViewById(R.id.cancle);
bind.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
myReceiver =
new
BroadCastActivity();
IntentFilter filter =
new
IntentFilter();
filter.addAction(MY_ACTION);
TestActivity.
this
.registerReceiver(myReceiver, filter);
System.out.println(
"bind"
);
Intent intent =
new
Intent();
intent.setAction(MY_ACTION);
intent.putExtra(
"msg"
,
"asdsdas"
);
sendBroadcast(intent);
}
});
unbind.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
TestActivity.
this
.unregisterReceiver(myReceiver);
System.out.println(
"unbind"
);
}
});
cancle.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
finish();
}
});
}
@Override
protected
void
onResume() {
// TODO Auto-generated method stub
super
.onResume();
}
}
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/bind"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:text=
"绑定"
/>
<Button
android:id=
"@+id/unbind"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:text=
"取消绑定"
/>
<Button
android:id=
"@+id/cancle"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:text=
"exit"
/>
</LinearLayout>
总结:我在测试时点击绑定怎么都不显示Toast的内容,原因是我只注册了广播,没有发送广播。要想广播响应,一定要注册还要发送!
- android中广播的两种启动方式浅析【自定义Action】
- Broadcast-----------自定义Action , 两种方式 实现广播Broadcast
- Broadcast-----------自定义Action , 两种方式 实现广播Broadcast
- android广播的两种方式!
- Android 广播注册的两种方式
- android广播的两种方式!
- Android广播的两种注册方式
- Android中activity的两种启动方式
- Android中自定义Dialog常用的两种方式
- Android中自定义Dialog布局的两种方式比较
- android注册广播的两种方式及优缺点
- Android 注册广播的两种方式区别
- Android 注册广播的两种方式区别
- android注册广播的两种方式及优缺点
- Android 注册广播的两种方式区别
- Android 的广播机制和两种注册方式
- Android 监听wifi广播的两种方式
- Android 注册广播的两种方式区别
- DebugTool 工具类
- 大话设计模式 适配器模式
- Javascript闭包真经
- 贝叶斯最优分类器的一个实例MATLAB代码
- php变量声明
- android中广播的两种启动方式浅析【自定义Action】
- 用adobe acrobat修改PDF文档中的文字
- ubuntu_细节
- STM32多中断模式
- vb.net 绘制sin函数
- 社交网络上的影响传播模型
- 侯捷先生整理的英繁简IT术语对照表
- jquery ajax 不执行success
- 输出时间