位置: 编程技术 - 正文

Activity和Service交互(service与activity数据交互)

编辑:rootadmin

推荐整理分享Activity和Service交互(service与activity数据交互),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:service与activity的区别,activity service,activity和service绑定,activity和service绑定,安卓activity和service,activity和application,activity和application,activity和service绑定,内容如对您有帮助,希望把文章链接给更多的朋友!

Activity和Service交互

本人常用的三种,不足之处希望大家多多提出宝贵意见

第一种Handler:

Activity

import android.annotation.SuppressLint;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ProgressBar;import com.service.MsghdSeceiver;public class MainHandlerActivity extends Activity { private ProgressBar mProgressBar; private Button btn; private Intent intent; @SuppressLint("HandlerLeak") Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { if (msg.what == 0x) { // 拿到进度,更新UI int progress = (int) msg.obj; mProgressBar.setProgress(progress); } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_handler); mProgressBar = (ProgressBar) findViewById(R.id.pb_mainhd_jindu); btn = (Button) findViewById(R.id.btn_mainhd_btn); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); MsghdSeceiver.setHandler(handler); // 启动服务 intent = new Intent("com.example.communication.MSG_ACTIONhd"); } @Override protected void onResume() { super.onResume(); setListener(); } private void setListener() { btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { startService(intent); } }); } @Override protected void onDestroy() { // 停止服务 stopService(intent); super.onDestroy(); }}

Service

import android.app.Service;import android.content.Intent;import android.os.Handler;import android.os.IBinder;import android.os.Message;public class MsghdSeceiver extends Service { /** * 进度条的最大&#; */ public static final int MAX_PROGRESS = ; /** * 进度条的进度&#; */ private int progress = 0; private static Handler handlers; public static void setHandler(Handler handler) { handlers = handler; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); startDownLoad(); } /** * 模拟下载任务,每秒钟更新一次 */ public void startDownLoad() { new Thread(new Runnable() { @Override public void run() { while (progress < MAX_PROGRESS) { progress &#;= ; Message message = new Message(); message.what = 0x; message.obj = progress; handlers.sendMessage(message); // Message.obtain(handlers, 0x, progress).sendToTarget(); try { Thread.sleep(); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; }}

注意

<service android:name="com.service.MsghdSeceiver" android:exported="false" > <intent-filter> <action android:name="com.example.communication.MSG_ACTIONhd" /> </intent-filter> </service>

第二种接口:

Activity

import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ProgressBar;import com.OnProgressListener;import com.service.MsgService;public class MainActivity extends Activity { private MsgService msgService; private ProgressBar mProgressBar; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mProgressBar = (ProgressBar) findViewById(R.id.pb_main_jindu); btn = (Button) findViewById(R.id.btn_main_btn); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); // 绑定Service 需要注册<action // android:name="com.example.communication.MSG_ACTION" /> Intent intent = new Intent("com.example.communication.MSG_ACTION"); bindService(intent, conn, BIND_AUTO_CREATE); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); setListener(); } private void setListener() { // TODO Auto-generated method stub btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub msgService.startDownLoad(); } }); } ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub msgService = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { // 返回一个MsgService对象 msgService = ((MsgService.MsgBinder) service).getService(); // 注册回调接口来接收下载进度的变化 msgService.setOnProgressListener(new OnProgressListener() { @Override public void onProgress(int progress) { // TODO Auto-generated method stub mProgressBar.setProgress(progress); } }); } }; @Override protected void onDestroy() { unbindService(conn); super.onDestroy(); }}

Activity和Service交互(service与activity数据交互)

Service

import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import com.OnProgressListener;public class MsgService extends Service { /** * 进度条的最大&#; */ public static final int MAX_PROGRESS = ; /** * 进度条的进度&#; */ private int progress = 0; /** * 更新进度的回调接口 */ private OnProgressListener onProgressListener; /** * 注册回调接口的方法,供外部调用 * * @param onProgressListener */ public void setOnProgressListener(OnProgressListener onProgressListener) { this.onProgressListener = onProgressListener; } /** * 增加get()方法,供Activity调用 * * @return 下载进度 */ public int getProgress() { return progress; } /** * 模拟下载任务,每秒钟更新一次 */ public void startDownLoad() { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (progress < MAX_PROGRESS) { progress &#;= ; // 进度发生变化通知调用方 if (onProgressListener != null) { onProgressListener.onProgress(progress); } try { Thread.sleep(); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } /** * 返回一个Binder对象 */ @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return new MsgBinder(); } public class MsgBinder extends Binder { /** * 获取当前Service的实例 * * @return */ public MsgService getService() { return MsgService.this; } }}

接口

public interface OnProgressListener { void onProgress(int progress);}

注意

<service android:name="com.service.MsgService" android:exported="false" > <intent-filter> <action android:name="com.example.communication.MSG_ACTION" /> </intent-filter> </service>

第三种广播:

Activityimport android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ProgressBar;public class MainGbActivity extends Activity { private ProgressBar mProgressBar; private Button btn; private Intent mIntent; private MsgReceiver msgReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_gb); mProgressBar = (ProgressBar) findViewById(R.id.pb_maingb_jindu); btn = (Button) findViewById(R.id.btn_maingb_btn); } @Override protected void onStart() { super.onStart(); // 启动服务 mIntent = new Intent("com.example.communication.MSG_ACTIONs"); // 动态注册广播接收器 msgReceiver = new MsgReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("com.example.communication.RECEIVER"); registerReceiver(msgReceiver, intentFilter); } @Override protected void onResume() { super.onResume(); setListener(); } private void setListener() { btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { startService(mIntent); } }); } /** * 广播接收器 * */ public class MsgReceiver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent intent) { // 拿到进度,更新UI int progress = intent.getIntExtra("progress", 0); mProgressBar.setProgress(progress); } } @Override protected void onDestroy() { super.onDestroy(); // 停止服务 stopService(mIntent); // 注销广播 unregisterReceiver(msgReceiver); }}

Service

import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MsggbSeceiver extends Service { /** * 进度条的最大&#; */ public static final int MAX_PROGRESS = ; /** * 进度条的进度&#; */ private int progress = 0; private Intent intent = new Intent("com.example.communication.RECEIVER"); /** * 模拟下载任务,每秒钟更新一次 */ public void startDownLoad() { new Thread(new Runnable() { @Override public void run() { while (progress < MAX_PROGRESS) { progress &#;= ; // 发送Action为com.example.communication.RECEIVER的广播 intent.putExtra("progress", progress); sendBroadcast(intent); try { Thread.sleep(); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub startDownLoad(); return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; }}

注意

<service android:name="com.service.MsggbSeceiver" android:exported="false" > <intent-filter> <action android:name="com.example.communication.MSG_ACTIONs" /> </intent-filter> </service>

布局一样

<RelativeLayout xmlns:android=" xmlns: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="com.activity.MainActivity$PlaceholderFragment" > <ProgressBar android:id="@&#;id/pb_mainhd_jindu" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@&#;id/btn_mainhd_btn" android:layout_marginTop="dp" android:text="HandlerService" android:layout_below="@id/pb_mainhd_jindu" android:layout_width="match_parent" android:layout_height="wrap_content" /></RelativeLayout>

注解差不多都了,一看就应该懂,最好写一下,记忆会深点

Android手机实时视频监控 最近手机安装了一个千里和千里家居远程监控的应用,对里面的实时监控交通路口的状态有点好奇。以是使用相应的方法反编译看看她的原理。对于软

解决“Cannot merge new index into a non-jumbo instruction”的问题 在将ADT和SDKTool升级到最新(分别是.1和.0.1)之后,我的一个工程(相对比较大)在编译并运行的时候,出现错误,Eclipse控制台输出如下信息:Unableto

android 功能引导界面实现 一.界面实现:借鉴了别人的实例,然后记录下引导界面的实现,总体来说实现不算困难,前提是要有个美工帮你做这些引导图片(找了张图片凑合用吧

标签: service与activity数据交互

本文链接地址:https://www.jiuchutong.com/biancheng/381062.html 转载请保留说明!

上一篇:Fragment里面嵌套ViewPager(fragment中嵌套fragment)

下一篇:Android手机实时视频监控(安卓实时屏幕)

  • 工资表怎么导入个税系统计算个税
  • 什么叫做供应链公司
  • 跨年增值税专用发票没有认证,需要开红字信息表吗
  • 补发工资怎么补发
  • 会计差错更正流程
  • 土地股权转让交多少税
  • 部分红冲的发票无法勾选怎么办
  • 季度末需要结转什么科目
  • 空报税怎么报
  • 样品赠送要确认收入吗
  • 广播影视服务需要交文化事业建设费吗
  • 费用摊销的常用方法有哪些
  • 租用办公室协议书
  • 技术服务费进项发票怎么做分录
  • 苹果电脑的删除
  • 费用跨年的分录怎么做
  • 职工福利费汇算清缴怎么填写
  • 苹果输入法快捷键怎么调出来
  • win10重启按什么键重置
  • 对数据文件操作,进行数据记录的交换都要经过
  • 应交所得税和所得税费用的区别计算公式
  • Linux Mount NTFS分区造成的权限问题如何解决?
  • 若依框架登录后跳转到指定页面
  • 未办理装修手续
  • fte文件怎么打开
  • 生育津贴差额账务处理
  • 会计准则 职工福利
  • 固定资产不计提折旧有什么影响
  • 大数据可视化前端界面模板
  • mysql跨库join
  • 新成立公司建账
  • js中写php代码
  • vue 登陆
  • php中input的用法
  • php socketio
  • 应付账款盘点表
  • 服装店快递费
  • 公司法关于公司变更后权利义务的规定
  • pnpnpn的工作原理
  • 应收应付款项
  • 小规模纳税企业销售货物或者提供应税劳务
  • 停车费定额发票有效期是多久
  • 个人所得税进一步改革方向
  • 工会筹备金和工会经费滞纳金计算一样吗
  • mysql复制数据到另一张表
  • sql server恢复
  • 公司两个股东变更为一个股东,需要交税么
  • 关于增值税调整的补充协议
  • 已计提完的固定资产怎么做账
  • 固定资产减值损失影响营业利润吗
  • 金融工具占比低
  • 利润表的期末余额怎么算出来的
  • 速动比率和流动比率怎么分析
  • 哪些合同需要缴税
  • 营业外收入在汇算清缴里填哪张表
  • 盈亏平衡点怎么计算公式
  • 微软宣布将GPT接入操作系统
  • 列举linux常用的版本
  • macos finder的应用程序列表在哪里
  • linux ioctrl
  • macbookpro中的文本编辑
  • win10如何设置默认应用语言
  • ie11forwindows10
  • Ext JS 4实现带week(星期)的日期选择控件(实战一)
  • 3d图形学原理
  • eevee引擎
  • shell脚本指南
  • 置顶聊天折叠怎么开启
  • jQuery Checkbox 全选 反选的简单实例
  • js特殊字符有哪些
  • shell脚本怎么写循环
  • javascript高级程序设计电子版
  • unity游戏开发的技术路线有哪些
  • js键盘事件有哪些?各自的作用如何
  • 从零开始学公文写作
  • jQuery ajax提交Form表单实例(附demo源码)
  • Android中Strings.xml使用占位符示例
  • 电子税务局登录方式
  • 彩票中500万自己能得多少
  • 金融商品转让是什么意思
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设