Android中broadcastreceiver发送广播信息
来源:互联网 发布:淘宝退差价是几天之内 编辑:程序博客网 时间:2024/05/16 16:20
一、结果效果:
输入要广播的信息,然后点击“发送广播”,可以在在Android的下滑页面看见广播形式的通知。
二、实现步骤:
1.首先同之前文档所述建立如下项目目录:HelloBroadcast.java与MainActivity.java,其中MainActivity.java控制main.xml,而HelloBroadcasrReceiver.java主要是取得之前的数据,并发送广播。
2.在AndroidManifest.xml中<application>下声明BroadcastReceiver,具体方式如下(这是静态注册,动态注册在java文件中):
<receiver android:name=".HelloBroadcastReceiver"> <intent-filter> <action android:name="com.HoD.action.BroadcastReceiverTest" /> </intent-filter> </receiver>
3.打开main.xml布局文件。设置控件,如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/et_broadcastContent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入广播内容" /> <Button android:id="@+id/btn_sendBroadcast" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="发送广播" /> </LinearLayout>
4.打开MainActivity.java文件,编写如下代码:
package com.HoD.broadcastreceiver;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {//因为步骤操作相对较多,故取得对象实例化信息与操作数据分开写,则需要存入变量private Context mContext;private Button btnSendBroadcast;private TextView etBroadcastContent;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);//mContext = this;//获取Button实例化对象,并设置监听事件btnSendBroadcast = (Button) findViewById(R.id.btn_sendBroadcast);btnSendBroadcast.setOnClickListener(new SendBroadcastClickListener());//获取TextView的实例化对象etBroadcastContent = (TextView) findViewById(R.id.et_broadcastContent);}//声明SendBroadcastClickListener类使用OnClickListener接口private class SendBroadcastClickListener implements OnClickListener{@Overridepublic void onClick(View v){//获取文本框内内容String content = etBroadcastContent.getText().toString().trim();if(content.length() < 1){//取出里面“Hint”字符串用Toast方法显示Toast.makeText(mContext, etBroadcastContent.getHint(), 1).show();return;}Intent intent = new Intent();//运行HelloBroadcatReceiverintent.setAction("com.HoD.action.BroadcastReceiverTest");intent.putExtra("content", content);sendBroadcast(intent);}}}
5.打开HelloBroadcastReceiver,删掉Activity后,继承BroadcastReceiver类,具体代码如下:
package com.HoD.broadcastreceiver;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class HelloBroadcastReceiver extends BroadcastReceiver{private Context context;@Override//使用onReceive()方法接收数据public void onReceive(Context context, Intent intent){this.context = context;showNotification(intent);}private void showNotification(Intent intent){//通过getSystemService()方法获取NotificationManager服务NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);//创建一个Notification对象,并为其设置各种属性//获取传递的图标、数据和当时的系统时间//这种方式已经过时,不过先以体会思想为主Notification notification = new Notification(R.drawable.ic_launcher, intent.getExtras().getString("content"), System.currentTimeMillis());//该语句的作用是定义了一个不是当即显示的activity,只有当用户拉下notify显示列表,并且单击对应的项的时候,才会触发系统跳转到该activityPendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);//notification.setLatestEventInfo(this, title, content, contentIntent);在此处设置在nority列表里的该norifycation得显示情况。notification.setLatestEventInfo(context, intent.getExtras().getString("content"), null, pendingIntent);//通过NotificationManager类的notify()方法将通知发送到状态栏notificationManager.notify(R.layout.main, notification);}}
6.遇到的问题就是包需要再加上一些,使用之前的方法,Clean一下就好了。
0 0
- Android中broadcastreceiver发送广播信息
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- Android中 广播BroadcastReceiver
- android中BroadCastReceiver使用(广播的接受和发送)
- android-BroadcastReceiver 发送一条普通广播
- Android-自定义发送/接收广播(BroadcastReceiver)
- Android BroadcastReceiver(广播)实现消息发送
- Android中广播BroadCastReceiver使用
- Android-广播BroadcastReceiver-自定义广播接受者和发送者
- BroadcastReceiver广播传递信息
- Android中用广播从Service中向Activity发送信息
- Android中用广播从Service中向Activity发送信息
- Android中用广播从Service中向Activity发送信息
- 10、Android中广播接收者-BroadcastReceiver
- include指令与include动作比较
- 如何利用IO拷贝图片
- 构建需求响应式亿级商品详情页
- stm32 HardFault_Handler调试及问题查找方法
- Shader之Photoshop Color效果
- Android中broadcastreceiver发送广播信息
- 凭兴趣求职80%会失败,为什么 ?
- Ubuntu Core在高通dragon board上的展示(视频)
- vc6.0转换为vs2008后代码编译不通过,error C2440 “static_cast” 无法从“void (__thiscall C* )(void)...
- 谈谈java线程
- UVa 1640 The Counting Problem
- 结构体的理解
- 进阶篇:3.2)钣金件设计指南
- 原码,反码,补码,移码