首页 技术 正文
技术 2022年11月15日
0 收藏 618 点赞 3,317 浏览 2376 个字

1.创建布局文件activity_broadcast.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".BroadcastActivity" > <Button
android:id="@+id/sendBroadcastIntent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Broadcast Intent"
/>
</RelativeLayout>

2.创建Activity

public class BroadcastActivity extends Activity {
//设定为com.china.ui.NEW_LIFEFORM,显示内容前,多一条信息"收到广播信息";
//public static final String MY_NEW_LIFEFORM="com.china.ui.NEW_LIFEFORM";
public static final String MY_NEW_LIFEFORM="NEW_LIFEFORM";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broadcast);
//传递数据
final Intent intent=new Intent(MY_NEW_LIFEFORM);
intent.putExtra("msgContent", "测试内容");
//初始化按钮
Button button=(Button) findViewById(R.id.sendBroadcastIntent);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
sendBroadcast(intent);
}
});
}
}

3.创建BroadcastReceiver

public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String content=intent.getStringExtra("msgContent");
CharSequence string="收到信息:"+content;
Toast.makeText(context, string, Toast.LENGTH_LONG).show();
}
}

4.配置AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dh.broadcast"
android:versionCode=""
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="" android:targetSdkVersion="" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.dh.broadcast.BroadcastActivity"
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="com.dh.broadcast.Receiver" android:enabled="true">
<intent-filter >
<!-- <action android:name="com.china.ui.NEW_LIFEFORM"/> -->
<action android:name="NEW_LIFEFORM"/>
</intent-filter>
</receiver>
</application>
</manifest>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922