- 浏览: 576293 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
hxlniada:
[color=darkred][/color][size=sm ...
利用Handler来更新android的UI -
jack_ye:
...
检测Android中触摸屏手势的动作与坐标 -
z278013421:
Android Services使用示例 -
东方vs不败:
写的不错,挺好的
利用Handler来更新android的UI -
liangxping:
就是双缓存机制而已。
Android中SurfaceView的使用示例
Android开发中,当需要创建在后台运行的程序的时候,就要使用到Service。Service 可以分为有无限生命和有限生命两种。
特别需要注意的是Service跟Activities是不同的(简单来说可以理解为后台与前台的区别),例如,如果需要使用Service的话,需要调用startService(),从而利用startService()去调用Service中的OnCreate()和onStart()方法来启动一个后台的Service。
启动一个Service的过程如下:
context.startService() ->onCreate()- >onStart()->Service running
其中onCreate()可以进行一些服务的初始化工作,onStart()则启动服务。
停止一个Service的过程如下:
context.stopService() | ->onDestroy() ->Service stop
接下来的实例是一个利用后台服务播放音乐的小例子,点击start运行服务,点击stop停止服务。
程序运行界面:
代码:
定义服务,MyService.java
package com.example; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class MyService extends Service { private static final String TAG = "MyService"; MediaPlayer player; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); Log.d(TAG, "onCreate"); player = MediaPlayer.create(this, R.raw.braincandy);//运行例子是,需要替换音乐的名称 player.setLooping(false); // Set looping } @Override public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); player.stop(); } @Override public void onStart(Intent intent, int startid) { Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); player.start(); } }
除此之外还要在Manifest里面声明服务:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ServicesDemo" 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:enabled="true" android:name=".MyService" /> </application> <uses-sdk android:minSdkVersion="3" /> </manifest>
定义Activity,ServicesDemo.java
package com.example; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ServicesDemo extends Activity implements OnClickListener { private static final String TAG = "ServicesDemo"; Button buttonStart, buttonStop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonStart = (Button) findViewById(R.id.buttonStart); buttonStop = (Button) findViewById(R.id.buttonStop); buttonStart.setOnClickListener(this); buttonStop.setOnClickListener(this); } public void onClick(View src) { switch (src.getId()) { case R.id.buttonStart: Log.d(TAG, "onClick: starting srvice"); startService(new Intent(this, MyService.class)); break; case R.id.buttonStop: Log.d(TAG, "onClick: stopping srvice"); stopService(new Intent(this, MyService.class)); break; } } }
翻译整理自:http://www.planetandroid.com/
- ServicesDemo.rar (26.5 KB)
- 下载次数: 1276
评论
7 楼
z278013421
2014-02-08
6 楼
飞扬云
2011-09-18
楼主 列举的例子很好,看起来很顺,很容易懂。
非常感谢了!
向你学习!
非常感谢了!
向你学习!
5 楼
zhikook
2010-03-02
很多好的例子,都是SDK ,不过大家很多学习角度的不同
4 楼
wafj1984
2010-02-10
这个不是apidemo里边有吗?
3 楼
happyljt
2010-01-27
raymondlueng 写道
wylsx 写道
停止一个Service的过程如下:
应该是 context.stopService() | ->Service stop ->onDestroy() 吧 呵呵!
应该是 context.stopService() | ->Service stop ->onDestroy() 吧 呵呵!
不是的,是context.stopService() | ->onDestroy() ->Service stop
你可以在onDestroy()中作一些资源回收的工作!
UP
2 楼
raymondlueng
2010-01-15
wylsx 写道
停止一个Service的过程如下:
应该是 context.stopService() | ->Service stop ->onDestroy() 吧 呵呵!
应该是 context.stopService() | ->Service stop ->onDestroy() 吧 呵呵!
不是的,是context.stopService() | ->onDestroy() ->Service stop
你可以在onDestroy()中作一些资源回收的工作!
1 楼
wylsx
2010-01-15
停止一个Service的过程如下:
应该是 context.stopService() | ->Service stop ->onDestroy() 吧 呵呵!
应该是 context.stopService() | ->Service stop ->onDestroy() 吧 呵呵!
发表评论
-
类似Chome的翻转效果
2012-09-18 23:14 2344简单的demo,屏幕上下滑动翻转View,类似Android ... -
3D编程基础数学之旋转及在Android中的应用
2012-02-10 00:11 0最近因为兴趣,看了一些3D编程的书,有种感觉就是写这些书的作者 ... -
Android中屏幕Density对BitmapDrawable的影响
2011-07-08 21:44 7844很早之前写的文章了,发在这里记录一下。 在之前的 ... -
Android程序与Javascript的交换
2010-04-16 21:40 0hello! -
Android游戏开发之服务器端
2010-01-09 16:38 6669首先我不知道这篇文章发到Android开发板块是否适合的,因为 ... -
Android ListView常用用法
2009-12-03 20:56 36599ListView是比较常用的控件,但一直都觉得创建ListVi ... -
Android View添加 Listener 小技巧示例
2009-12-02 15:35 9788在开发中为控件添加Listener是非常常见的工作,最简单的添 ... -
Android Gesture 手势识别使用实例
2009-10-20 11:56 10151很高兴能在Android1.6的sdk看到手势识别这一 ... -
Android第二个游戏 DragonFeed(勇者喂恶龙)
2009-10-12 17:57 7963这是参加学校一个小比赛的参赛游戏,之前一直在忙 认证考试和项目 ... -
Android 震动示例--心跳效果
2009-10-02 23:58 7673正在开发第二个游戏,计时就要结束的时候,为了营造紧张的气氛, ... -
Android adb的使用略解
2009-09-30 16:08 40218安卓网的一个帖子,写得不错,记下来 ADB全称Android ... -
Android游戏开发之连连看算法
2009-09-29 00:36 10347因为有朋友在站内信中问到连连看的具体算法,所以我就把算法pos ... -
android游戏开发之选关画面
2009-09-21 15:44 3974在游戏开发中,往往要提供选关的页面,选择关卡可以简单地使用li ... -
Android重力感应Demo
2009-09-11 23:33 13989android中的很多游戏的游戏都使用了重力感应的技术,但其a ... -
Android游戏开发之爆炸效果
2009-09-02 00:29 18355在做Android游戏MagicBubble开发的时候 ... -
Android 的第一个游戏
2009-08-18 08:48 7872学了android1个多月,做了一款连连看游戏,上了andro ... -
Android canvas的save()和restore()用法
2009-08-05 12:21 19830在读Android SDK里面的samples里面游戏的时候, ... -
Android 2d物理引擎Box2d的使用示例
2009-08-04 09:06 14197Box2d是一个很出名的2d物理引擎,大家可以google之。 ... -
利用Handler更新Android UI的另一种方法
2009-07-29 11:28 11846文字程序很简单,一个View,一个Activity,利用han ... -
android布局学习利器-Hierarchy Viewer
2009-07-27 18:17 8068Hierarchy Viewer 帮你分析应用程序UI布局 ...
相关推荐
android访问webservices详细例子 供大家参考学习
是一个android services的示例代码
Android查询来电归属地示例,利用Web Services调用服务进行来电归属地查询
Android 学习资料上面的例子 Ch 3 - Your First Android Application Ch 4 - Application Lifecycle Ch 5 - Resources Ch 6 - Views Ch 7 - Layout Ch 8 - Drawing and Animation Ch 9 - Files and Databases Ch 10 ...
android4.0 api的相关例子,包括: Notifications Alarms Progress Dialogs Intents Menus Search Persistent application state Preferences Background Services App Widgets Voice Recognition 可以直接...
一组演示googleapi的快速启动示例
The intermediate course is more complex includes material design, sqlite, processes in second plane, practice applications, google services and more. we hope that you learn a lot and become a
7个android开发的例子,包括intent, sqlite3, services, activity等
参考这两个例子就可以写出 调用 java 或.net 的webservice
android开发的多个例子,android studio打开,包括ui,资源读取,webview等等20多个例子程序,这是例子名称:ActionBarCompatEclipse,AnimationTest,AntiPatternExamples,AppOps,BottomListProject,...
page_type 语言产品样本Java天蓝色天蓝色通信服务群组通话样本该示例是一个本地Android应用程序,该应用程序使用Azure Communication Services Android客户端库来构建具有语音和视频通话功能的通话体验。 该应用程序...
google-location-services-api 使用 Android 的 Google 位置服务 API 确定用户纬度和经度的示例 Android 应用程序
这是有关服务和通知的Android演示,包括: 使用JobIntentService 使用ResultReceiver在JobIntentService和Activity之间进行通信 使用AlarmManager 与ImageDownloadService异步下载映像 在和指南的悬崖笔记中阅读...
Android从入门到精通书中的例子 希望对大家有帮助 Ch 3 Your First Android Application Ch 4 Application Lifecycle Ch 5 Resources Ch 17 Services
在android平台上使用google tasks api的示例程序,包含google play services 依赖库
包括FireBase集成,您需要将google-services.json文件添加到项目中,然后才能使用它。 转到创建新项目。 您可以从Google Play下载应用程序: : id=me.lolevsky.nasaplanetary 您可以在有缺陷的文章中找到有关该...
要将示例应用程序添加到Firebase项目,请使用在该applicationId的app/build.gradle文件中指定的applicationId值作为Android包名称。 下载生成的google-services.json文件,然后将其复制到您要运行的示例的app/目录...
特性和对象的集合,这些特性和对象使在Scala中为Android编程时更方便使用。 问题/贡献 请注意,到目前为止,该库尚未经过大量测试。 请使用GitHub 报告任何问题或改进。 这也意味着该库可能仍会进行重大更改。 ...
该存储库由一个客户端和两个服务器组件组成: : Android示例应用程序,显示了Google Play服务对设备上的SafetyNet API的使用。 :两个示例,显示了如何在Java服务器上验证SafetyNet API响应,包括通过Android ...
compile'com.google.android.gms:play-services:7.0.0'(Google_play_services SDK) Cowley SDK(3.3.30版),Android API 7或更高版本 AdFit SDK(版本2.3.4),Android API 9或更高版本 授予应用程序的...