android广播机制(上)
来源:互联网 发布:电脑优化加速软件 编辑:程序博客网 时间:2024/06/05 23:00
android的广播机制主要分为两个部分,就想我们听的广播一样,第一给部分是发送广播,第二个部分是接受广播。
代码还是很简单的。
==============TestBCActivity.java================
package com.example.testbc;
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 TestBCActivity extends Activity {
private Button bcButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_bc);
bcButton = (Button) findViewById(R.id.BCButton);
bcButton.setOnClickListener(new BCButtonListenner());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test_bc, menu);
return true;
}
class BCButtonListenner implements OnClickListener{
@Override
public void onClick(View arg0) {//最主要的在这里,这是发送的部分
Intent intent = new Intent();
intent.setAction(Intent.ACTION_EDIT);//可以认为这是发送时的类型,也就是接收的时候也是这个类型的才接受
TestBCActivity.this.sendBroadcast(intent);//发送广播
}
}
}
====================以下是接收的====================
==========TestReceiver.java============
package com.example.testbc;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class TestReceiver extends BroadcastReceiver {//首先要继承BroadcastReceiver方法
public TestReceiver() {
System.out.println("TestReceiver");
}
@Override
public void onReceive(Context arg0, Intent arg1) {//在接收时,先运行构造函数,再运行onReceive,最后在onReceive运行完之后,android系统会使receiver销毁。
System.out.println("onReceive");
}
}
==========AndroidManifest.xml============
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testbc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testbc.TestBCActivity"
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><!-- 决定接受什么类型的Receiver 这里就对应最前面一点代码中的“ intent.setAction(Intent.ACTION_EDIT)”这句话-->
<action android:name="android.intent.action.EDIT" />
</intent-filter>
</receiver>
</application>
</manifest>
- android广播机制(上)
- Android广播机制概述(上)
- Android广播机制(1)
- Android广播机制(2)
- Android广播机制(下)
- Android 广播机制(一)
- [Android]Android广播机制
- Android 四大组件 —— 广播(广播机制解析)
- Android 四大组件 —— 广播(广播机制解析)
- Android中的广播机制(一)----- 接收广播
- Android中的广播机制(二)----- 发送广播
- Android开发-广播机制(一)
- Android学习之广播机制(1)
- Android学习之广播机制(2)
- Android学习(10)-广播机制
- Android笔记(二十四)广播机制
- Android广播机制(很详细)
- Android消息推送(广播机制)+通知
- BFS POJ2243- Dario
- LeetCode OJ:Reverse Integer
- centos 下 boost1.55 编译和安装
- ODBC-C语言连接数据库(SQL Server 2000)(上)
- jQuery操作Dom
- android广播机制(上)
- 解决MAC打开不明软件问题,如【打不开“FreeMind.app”,因为它来自身份不明的开发者。 】
- cvThreshold用法(opencv参考手册)
- Ubuntu install cf cli
- 给自定义组件加事件
- [Zjoi2013]K大数查询 整体二分/树套树
- [DFS]FJSDFZOJ 1423 n皇后
- FOJ 2129 子序列个数
- PAT 1017 Queueing at Bank