Android开发之发送自定义广播
来源:互联网 发布:广告图片制作软件 编辑:程序博客网 时间:2024/05/22 17:31
定义广播接收器
package com.example.mybroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "received is MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
}
}
在AndroidManifest.xml 对这个广播器接收器进行注册
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
在activity_main.xml中添加一个按钮
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Broadcast"
/>
修改MainActivity.java代码
package com.example.mybroadcastreceiver;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");
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开发之发送自定义广播
- Android广播之发送自定义广播+本地广播(二)
- 安卓开发之发送自定义广播
- android-发送自定义广播
- Android 发送自定义广播
- Android 发送自定义广播
- Android之广播三(发送自定义广播)
- Android开发之发送有序广播+拦截广播
- 发送自定义广播 之 标准广播
- 发送自定义广播 之 有序广播
- Android 发送系统广播与自定义广播
- Android自定义发送无序广播
- Android自定义发送有序广播
- Android自定义发送本地广播
- Android之BroadcastReceiver(三):发送自定义广播
- android开发 自定义广播
- Android开发之使用Intent发送广播消息(1)
- android之broadcast发送广播
- Android实现类淘宝多图评价
- segmentControl实现控制器的切换
- 2016年元月份腾讯测试开发补录面试总结
- 自定义AsyncTaskLoader
- UESTC 1063 秋实大哥与妹纸 二叉堆(大根树)
- Android开发之发送自定义广播
- DB2 基础系列文章
- 杭电1161Eddy's mistakes
- servlet处理客户请求,MVC设计模式
- 定位权限是否开启
- NSUserDefaults
- poj 3614 Sunscreen 贪心
- SpringMVC中使用Interceptor拦截器
- Java for Web学习笔记(十七):Session(1)Session的携带