Android开发之使用本地广播
来源:互联网 发布:尼康d750wifi软件下载 编辑:程序博客网 时间:2024/06/06 07:07
直接代码,不解释,看不明白,看前面几篇,大牛就别看了
package com.example.mybroadcastreceiver;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private IntentFilter intentFilter;
private LocalReceiver localReceiver;
private LocalBroadcastManager localBroadcastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
localBroadcastManager=LocalBroadcastManager.getInstance(this); //获取实例
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.LOCAL_BROADCAST");
//sendBroadcast(intent);
//sendOrderedBroadcast(intent,null);
localBroadcastManager.sendBroadcast(intent); //发送本地广播
}
});
intentFilter=new IntentFilter();
intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
localReceiver=new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver, intentFilter); //注册本地广播监听器
}
protected void onDestroy()
{
super.onDestroy();
localBroadcastManager.unregisterReceiver(localReceiver);
}
class LocalReceiver extends BroadcastReceiver{
public void onReceive(Context context,Intent intent)
{
Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show();
}
}
@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之BroadcastReceiver(四):使用本地广播
- Android广播之发送自定义广播+本地广播(二)
- Android开发之广播
- 【Android】安卓学习笔记之广播(四)使用本地广播
- 安卓开发之本地广播
- Android(安卓)使用本地广播
- Android学习之发送及接收广播及本地广播
- Android开发之使用Intent发送广播消息(1)
- Android开发之局部广播的使用——LocalBroadcast
- Android开发之广播机制
- Android开发之广播机制
- 浅谈Struts2拦截器的原理与实现
- 1056. Mice and Rice (25)
- 1006. Sign In and Sign Out (25)
- C++学习之构造函数、析构函数
- 【libgdx】2D地图块(tileset)地图出现缝隙(gap / bleeding)的问题
- Android开发之使用本地广播
- python中一些常见的针对字符串的操作
- 成员函数
- 命令行运行jar 并传参数
- Android早期回顾,不定时增加,回顾以往,加深理解
- OpenGL实践之第一个窗口
- Android干净架构详解:为你的应用打造一个清爽的架构!
- oracle判断是否为数字或数字型字符串
- CGAL join_face source code analysis