`
xihuan&java
  • 浏览: 159634 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

android开发

阅读更多
原地址http://www.dnbcw.com/biancheng/java/lfwk176836.html

//设置高宽
// LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(    
//                    LinearLayout.LayoutParams.FILL_PARENT,    
//                    LinearLayout.LayoutParams.WRAP_CONTENT    
//            );

简介:这是Activity和Service之间通讯的Demo的详细页面,介绍了和java,JavaEye Activity和Service之间通讯的Demo有关的知识,加入收藏请按键盘ctrl+D,谢谢大家的观看!要查看更多有关信息,请点击此处
1.Activity的类别文件:

package wyf.wpf;//声明包语句

import 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.TextView;
//继承自Activity的子类

public class Sample_3_6 extends Activity {
public static final int CMD_STOP_SERVICE = 0;
Button btnStart;// 开始服务Button对象应用
Button btnStop;// 停止服务Button对象应用
TextView tv;// TextView对象应用
DataReceiver dataReceiver;// BroadcastReceiver对象

@Override
public void onCreate(Bundle savedInstanceState) {// 重写onCreate方法
super.onCreate(savedInstanceState);
setContentView(R.layout.main);// 设置显示的屏幕
btnStart = (Button) findViewById(R.id.btnStart);
btnStop = (Button) findViewById(R.id.btnStop);
tv = (TextView) findViewById(R.id.tv);

btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(Sample_3_6.this,
wyf.wpf.MyService.class);
startService(myIntent);// 发送Intent启动Service
}
});

btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction("wyf.wpf.MyService");
myIntent.putExtra("cmd", CMD_STOP_SERVICE);
sendBroadcast(myIntent);// 发送广播
}
});
}

private class DataReceiver extends BroadcastReceiver {// 继承自BroadcastReceiver的子类
@Override
public void onReceive(Context context, Intent intent) {// 重写onReceive方法
double data = intent.getDoubleExtra("data", 0);
tv.setText("Service的数据为:" + data);
}
}

@Override
protected void onStart() {// 重写onStart方法
dataReceiver = new DataReceiver();
IntentFilter filter = new IntentFilter();// 创建IntentFilter对象
filter.addAction("wyf.wpf.Sample_3_6");
registerReceiver(dataReceiver, filter);// 注册Broadcast Receiver
super.onStart();
}

@Override
protected void onStop() {// 重写onStop方法
unregisterReceiver(dataReceiver);// 取消注册Broadcast Receiver
super.onStop();
}
}


2.服务的类:

package wyf.wpf;//声明包语句

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
//继承自Service的子类

public class MyService extends Service {
CommandReceiver cmdReceiver;
boolean flag;

@Override
public void onCreate() {// 重写onCreate方法
flag = true;
cmdReceiver = new CommandReceiver();
super.onCreate();
}

@Override
public IBinder onBind(Intent intent) {// 重写onBind方法
// TODO Auto-generated method stub
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {// 重写onStartCommand方法
IntentFilter filter = new IntentFilter();// 创建IntentFilter对象
filter.addAction("wyf.wpf.MyService");
registerReceiver(cmdReceiver, filter);// 注册Broadcast Receiver,后续会接收相关广播intent
doJob();// 调用方法启动线程
return super.onStartCommand(intent, flags, startId);
}

// 方法:
public void doJob() {
new Thread() {
public void run() {
while (flag) {
try {// 睡眠一段时间
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
Intent intent = new Intent();// 创建Intent对象
intent.setAction("wyf.wpf.Sample_3_6");
intent.putExtra("data", Math.random());
sendBroadcast(intent);// 发送广播
}
}

}.start();
}

private class CommandReceiver extends BroadcastReceiver {// 继承自BroadcastReceiver的子类
@Override
public void onReceive(Context context, Intent intent) {// 重写onReceive方法
int cmd = intent.getIntExtra("cmd", -1);// 获取Extra信息
if (cmd == Sample_3_6.CMD_STOP_SERVICE) {// 如果发来的消息是停止服务
flag = false;// 停止线程
stopSelf();// 停止服务
}
}
}

@Override
public void onDestroy() {// 重写onDestroy方法
this.unregisterReceiver(cmdReceiver);// 取消注册的CommandReceiver
super.onDestroy();
}
}


最后是配置文件和main.xml,每个Service都需要在配置文件里申明标签:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wyf.wpf" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Sample_3_6" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" android:process=":remote">
<intent-filter>
<action android:name="wyf.wpf.MyService" />
</intent-filter>
</service>
</application>
<uses-sdk android:minSdkVersion="7" />

</manifest>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btnStart" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="启动服务" />
<Button android:id="@+id/btnStop" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="停止服务" />
<TextView android:id="@+id/tv" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="等待来自Service的数据" />
</LinearLayout>
分享到:
评论

相关推荐

    阿里巴巴Android开发手册正式版1.0.1

    《阿里巴巴 Android 开发手册》是阿里巴巴集团各大 Android 开发团队的集体智慧结晶和经验总结,将淘宝、天猫、闲鱼、钉钉等 App 长期开发迭代和优化经验系统地整理成册,以指导 Android 开发者更加高效、高质量地...

    Android开发艺术探索 pdf 下载 高清完整版版).pdf

    《Android开发艺术探索》是一本Android进阶类书籍,采用理论、源码和实践相结合的方式来阐述高水准的Android应用开发要点。《Android开发艺术探索》从三个方面来组织内容。第一,介绍Android开发者不容易掌握的一些...

    新版Android开发教程.rar

    Android Android Android Android 开发背景 � 计算技术、无线接入技术的发展,使嵌入式系统逐渐有能力对桌面系统常规业务进行支持。 � 谷歌长期以来奉行的移动发展战略:通过与全球各地的手机制造商和移动运营商...

    Android开发教程(完整版)

    新版Android开发教程&笔记--基础入门一.pdf 新版Android开发教程&笔记--基础入门二.pdf 新版Android开发教程&笔记三--环境搭建与解析.pdf 新版Android开发教程&笔记四--Dalvik ADB.pdf 新版Android开发教程+笔记五--...

    老罗android开发视频教程全集百度网盘下载

    Android 是Google开发的基于Linux平台的开源手机操作系统。它包括操作系统、用户界面和应用程序—— 移动电话工作所需的全部软件,而且不存在任何...【第一版第十五章】老罗Android开发视频--百度地图实战开发(10集)

    大话企业级Android开发

    01大话企业级Android开发第一篇 02大话企业级Android开发 03大话企业级Android开发开发流程及项目管理 04大话企业级Android开发_Android项目的目录结构、执行流程及其他基础分析 05大话企业级Android开发_MVC讲解及...

    Android开发案例驱动教程 配套代码

    《Android开发案例驱动教程》 配套代码。 注: 由于第12,13,14章代码太大,无法上传到一个包中。 这三节代码会放到其他压缩包中。 作者:关东升,赵志荣 Java或C++程序员转变成为Android程序员 采用案例驱动模式...

    android开发入门教程

    第2章 工欲善其事 必先利其器——搭建Android开发环境 2.1 开发Android应用前的准备 2.1.1 Android开发系统要求 2.1.2 Android软件开发包 2.1.3 其他注意事项 2.2 Windows开发环境搭建 2.2.1 JDK、Eclipse、Android...

    Android开发艺术探索

    《Android开发艺术探索》是一本Android进阶类书籍,采用理论、源码和实践相结合的方式来阐述高水准的Android应用开发要点。《Android开发艺术探索》从三个方面来组织内容。第一,介绍Android开发者不容易掌握的一些...

    android开发艺术探索超清版

    android开发艺术探索超清版,绝对清晰 Android开发艺术探索》是一本Android进阶类书籍,采用理论、源码和实践相结合的方式来阐述高水准的Android应用开发要点。《Android开发艺术探索》从三个方面来组织内容。第一,...

    android开发期末大作业.zip

    android开发期末大作业(项目源码,任务书,实验大报告,apk文件) 大作业的要求和内容:(包括题目选择范围、技术要求、递交时间、考核方法等) 一、实验项目名称 Android手机应用开发课程大作业 二、实验目的 1....

    android开发常用图片

    比较全的android开发常用图片

    Android开发应用从入门到精通光盘

    Android开发应用从入门到精通 朱桂英 中国铁道出版社 本书循序渐进地讲解了android技术的基本知识,并通过实例直观地演示了android在各个领域中的具体应用。本书内容新颖、知识全面、讲解详细,全书分为4篇17章,第...

    Android开发艺术探索之高清版本

    android开发艺术探索超清版(Word转换成PDF的,非常清晰) 《Android开发艺术探索》是一本Android进阶类书籍,采用理论、源码和实践相结合的方式来阐述高水准的Android应用开发要点。《Android开发艺术探索》从三个...

    Android开发教程详细版.pdf

    Android开发教程详细版.pdf Android开发教程详细版.pdf

    Android开发PPT(PDF)

    Android开发PPT(PDF) 06_Android的GUI系统.pdf 07_Android的Audio系统.pdf 08_Android的Video_输入输出系统.pdf 09_Android的多媒体系统.pdf 10_Android的电话部分.pdf 10大话企业级Android开发_组件(上)....

    企业级安卓开发_入门+进阶 大话企业级Android开发

    01大话企业级Android开发第一部分_简介 02大话企业级Android开发第二部分_环境搭 03大话企业级Android开发开发流程及项目管理 04大话企业级Android开发_Android项目的目录结构、执行流程及其他基础分析 05大话企业级...

    Android开发权威指南源码。

    Android开发权威指南源码。

    Android开发艺术探索高清PDF(带目录)

    Android开发艺术探索高清PDF Android开发艺术探索高清PDF

    Android开发案例驱动教程 chapter13-17

    Android开发案例驱动教程 chapter13-17 关东升 , 赵志荣 《Android开发案例驱动教程》旨在帮助读者全面掌握Android开发技术,能够实际开发Android项目。《Android开发案例驱动教程》全面介绍了在开源的手机平台...

Global site tag (gtag.js) - Google Analytics