手工注册广播
来源:互联网 发布:sql execute 编辑:程序博客网 时间:2024/04/29 00:13
(不推荐)
可以通过代码完成广播的注册
在main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal">
<Button
android:id="@+id/mybut"
android:layout_marginTop="8dp"
android:layout_width="80dp"
android:layout_height="40dp"
android:textColor="#ffffff"
android:background="#3399ff"
android:text="开始广播"/>
</LinearLayout>
新建MyBroadCastReceviceUtil.java:
package com.li.broadcastproject;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiverUtil extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("org.lxh.action.LYW".equals(intent.getAction())) { // 指定的Action
String msg = intent.getStringExtra("msg") ; // 取得附加信息
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
}
在MyBroadcastDemo.java中:
package com.li.broadcastproject;
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 MyBroadcastDemo extends Activity {
private Button mybut = null ;
private MyBroadcastReceiverUtil broadUtil = null ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mybut = (Button) super.findViewById(R.id.mybut);
this.mybut.setOnClickListener(new OnClickListenerImpl()) ;
}
private class OnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
Intent it = new Intent("org.lxh.action.LYW"); // 操作的过滤
it.putExtra("msg", "Hello 李叶文") ; // 附加信息
IntentFilter filter = new IntentFilter("org.lxh.action.LYW") ;
MyBroadcastDemo.this.broadUtil = new MyBroadcastReceiverUtil() ;
MyBroadcastDemo.this.registerReceiver(MyBroadcastDemo.this.broadUtil, filter) ;
MyBroadcastDemo.this.sendBroadcast(it) ;
}
}
}
- 手工注册广播
- 注册广播
- 注册广播
- 注册广播
- 手工注册BHO
- 如何手工注册MSComm控件
- 如何手工注册MSComm控件
- 使用 MemberShip 进行手工注册
- 如何手工注册MSComm控件
- Android开发 --- 广播 注册
- android 静态注册广播
- android 动态注册广播
- android动态注册广播
- 广播注册过程分析
- 广播接收器动态注册
- Android 广播注册方式
- 利用服务注册广播
- 动态注册广播接收器
- D3D9 Samples(6)--Meshes
- Linux Shell高级技巧(五)
- asp.net mvc controller 依赖注入入门
- l两个字符串相关问题
- POJ 2406 KMP
- 手工注册广播
- 播放器代码片段
- 何小鹏:轻应用,不只是Web App
- Linux Shell常用技巧(一)
- C#.NET学习笔记7--11---算术运算符,变量赋值,变量的交换,布尔表达式1,布尔表达式2
- Activity启动广播,广播启动服务
- Linux Shell常用技巧(二)
- myeclipse使用经验总结
- input标签里添加相关属性详解