Android之BroadcastReceiver1
2014-04-07 12:04
316 查看
1.触发发送广播
2.注册广播
3.定义接收广播类
public class MainActivity extends Activity { private Button sendButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendButton = (Button) findViewById(R.id.sendButton); sendButton.setOnClickListener(new BroadcastListener()); /* * if (savedInstanceState == null) { * getSupportFragmentManager().beginTransaction() .add(R.id.container, * new PlaceholderFragment()).commit(); } */ } class BroadcastListener implements OnClickListener { @Override public void onClick(View v) { TestReceiver tr = new TestReceiver(); Intent intent = new Intent(); intent.setAction(Intent.ACTION_EDIT); MainActivity.this.sendBroadcast(intent); } }
2.注册广播
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mars_1700_broadcast" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.mars_1700_broadcast.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=".TestReceiver"> <intent-filter> <action android:name="android.intent.action.EDIT"/> </intent-filter> </receiver> </application> </manifest>
3.定义接收广播类
package com.example.mars_1700_broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class TestReceiver extends BroadcastReceiver{ public TestReceiver(){ System.out.println("TestReceiver"); } @Override public void onReceive(Context context, Intent intent) { System.out.println("onReceive"); } }
相关文章推荐
- Android UI开发专题(三) 各种Drawable
- android检测网络状态
- 读取android手机流量信息
- 五款热门Android手机性能测试 Nexus S大胜
- android的PreferenceActivity2
- android.provider.ContactsContract ㄧ些資料庫欄位
- (转)图文详解手把手教你在Windows环境下下载Android源码(Launcher为例)
- Android 之 远程图片获取和本地缓存
- ERROR:Application requires API version 12.Device API version is 11 (Android 3.0).
- Android 资料整理
- Android的按钮单击事件及监听器的实现方式
- Android 打包签名
- Android UI 使用更快更高效
- android 竖屏显示
- Android调用WebService(ksoap2 天气预报)
- 每个android页面的.xml文件详解
- Android中解决图像解码导致的OOM问题
- android单元测试
- Android SYSTEM 系统文件夹结构解析
- Android布局充分演示Demo