- 浏览: 150345 次
- 性别:
- 来自: 茂名
文章分类
最新评论
-
stoneyulu:
很好,正需要要!用来在thread和activity之间传数 ...
在Activity中通过Bundle传递自定义数据类型 -
鱼在陆地上跑:
只能读取TXT格式的文件,而不能读取图片
读取Rescource和asset文件夹中的文件 -
myg_315:
我也在调查这个问题,谢谢!
Android程序的退出终于完美解决,兼容所有SDK -
jk138:
谢谢各位的评论,楼下的建议很好,值得大家注意了.
Android程序的退出终于完美解决,兼容所有SDK -
cyilinycc:
非常感谢你提供的这个方法,但我经过测试之后,发现这个方法存在弊 ...
Android程序的退出终于完美解决,兼容所有SDK
第一步.MainActivity.java
package com.chaowen; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button startButton,stopButton,bindButton,unbindButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startButton = (Button)findViewById(R.id.startButton); stopButton = (Button)findViewById(R.id.stopButton); bindButton = (Button)findViewById(R.id.bindButton); unbindButton = (Button)findViewById(R.id.unbindButton); //添加监听器 startButton.setOnClickListener(startListener); stopButton.setOnClickListener(stopListener); bindButton.setOnClickListener(bindListener); unbindButton.setOnClickListener(unbindListener); } //启动Service监听器 private OnClickListener startListener = new OnClickListener() { @Override public void onClick(View v) { //创建Intent Intent intent = new Intent(); //设置Action属性 intent.setAction("com.chaowen.action.MY_SERVICE"); //启动该Service startService(intent); } }; //停止Service监听器 private OnClickListener stopListener = new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction("com.chaowen.action.MY_SERVICE"); stopService(intent); } }; //连接对象 private ServiceConnection connection =new ServiceConnection() { //断开时调用 @Override public void onServiceDisconnected(ComponentName name) { //输出日志 Log.i("Service", "断开连接"); //通过Toast显示信息 Toast.makeText(MainActivity.this, "断开连接", Toast.LENGTH_LONG).show(); } //连接时调用 @Override public void onServiceConnected(ComponentName name, IBinder service) { //输出日志 Log.i("Service", "连接成功"); //通过Toast显示信息 Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_LONG).show(); } }; //绑定Service监听器 private OnClickListener bindListener = new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction("com.chaowen.action.MY_SERVICE"); bindService(intent, connection, Service.BIND_AUTO_CREATE); } }; //解除绑定Service监听器 private OnClickListener unbindListener = new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction("com.chaowen.action.MY_SERVICE"); unbindService(connection); } }; }
第二步.MyService.java
package com.chaowen;
import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class MyService extends Service { @Override public IBinder onBind(Intent intent) { Log.i("SERVICE", "onBind"); Toast.makeText(MyService.this, "onBind...", Toast.LENGTH_LONG).show(); return null; } @Override public void onCreate() { Log.i("SERVICE", "onCreate"); Toast.makeText(MyService.this, "onCreate...", Toast.LENGTH_LONG).show(); } @Override public void onStart(Intent intent, int startId) { Log.i("SERVICE", "onStart"); Toast.makeText(MyService.this, "onStart...", Toast.LENGTH_LONG).show(); } @Override public void onDestroy() { Log.i("SERVICE", "onDestroy"); Toast.makeText(MyService.this, "onDestroy...", Toast.LENGTH_LONG).show(); } }
第三步.main.xml
<?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:text="启动Service" android:id="@+id/startButton" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="停止Service" android:id="@+id/stopButton" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="绑定Service" android:id="@+id/bindButton" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="解除绑定" android:id="@+id/unbindButton" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
第四步.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.chaowen" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="4" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" 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"> <intent-filter> <action android:name="com.chaowen.action.MY_SERVICE" /> </intent-filter> </service> </application> </manifest>
发表评论
-
Failed to receive access token
2013-04-24 12:52 1452在 com.weibo.net.Token.java文件中 ... -
Android实例五:Notification通知和Broadcast结合
2011-06-15 11:42 8263第一步.MainActivity.java pack ... -
Android实例四:BroadCast_Receiver
2011-06-15 10:36 6728第一步. MainActivity.java ... -
Android实例二:手机数据库SQListe
2011-06-14 11:16 2156[code="java"] ... -
Android实例一:文件搜索
2011-06-13 20:12 3763本例子参考SDK开发范例大全 说明:以ListA ... -
关于android ListView的美化
2011-05-31 08:36 960用心的朋友应该会发现,listview中在设置了背景之后。会有 ... -
Android程序的退出终于完美解决,兼容所有SDK
2011-05-30 16:23 3662上网学了好多方法,关于Android的Activity如何退出 ... -
android兼容2.2及以前版本的退出方法
2011-05-30 14:16 1201首先,在首个activity中写: Intent inten ... -
缩放图片的方法
2011-05-26 15:35 1072代码一: public Bitmap ResizeBi ... -
Android对图片的压缩读取和保存
2011-05-26 12:46 1720在开发图片浏览器等软件是,很多时候要显示图片的缩略图,而一般情 ... -
android显示单位
2011-05-19 16:10 956px (pixels)像素 一般HVGA代表320 ... -
申请Google Map密钥
2011-05-17 15:25 17081.启动eclipse,依次单 ... -
SharedPreferences
2011-05-17 14:16 1253package wyf.wpf;//声明包语句 imp ... -
Content Provider
2011-05-17 11:54 1092package wyf.wpf; //声明包 ... -
轻量级数据库SQLite
2011-05-17 11:29 1186MySQLiteHelper.java im ... -
读取Rescource和asset文件夹中的文件
2011-05-17 11:01 5196package wyf.wpf; //声明包 ... -
数据存储之IO
2011-05-17 10:54 959package wyf.wpf; import jav ... -
android 检测当前网络并调用系统设置
2011-05-12 10:07 1668/** * 检测网络是否可用 */ pu ... -
Android 菜单(Menu)控件的使用
2011-05-11 14:05 712有两种方法,可以创建。 1.通过xml来创建 ... -
GridView宫格图的使用
2011-05-11 10:18 2525学习内容:1.GridView的使用 ...
相关推荐
全书分为18章,分别讲解了UI布局实例集锦、控件实例集锦、自动化服务实例集锦、数据存储实例集锦、电话和短信实例集锦、图形图像实例集锦、和网络有关的实例集锦、多媒体实例集锦、Google地图实例集锦、GoogleAPI...
android service 简单实例代码 . binder继承一个接口,实现方法。
关于Android Service的简单实例:属于start service类型,而不是bound service。
android intent service 实例android intent service 实例android intent service 实例
android service实例
博文android基础知识05:四大组件之service 02:远程调用中关于远程调用service的实例程序
android service的实例程序,用于博文http://blog.csdn.net/xianming01/article/details/7341877
Android Service实例 Android Service 一般处理比较耗时的操作
Android service 开发实例。欢迎大家下载学习
Android 的 Remote Service 开发实例RemoteService
有四个小实例 分别是安卓 service的 有启动方式的 绑定方式的 线程方式的 aidl方式的 看了这四个应该更加容易的懂了
根据博客内容,写的一个android 服务实例程序
10.1、Service:Service的创建、配置、启动、停止、绑定和通信; Service的生命周期; 10.2、跨进程调用Service(AIDL服务):创建AIDL文件; 将接口暴露给客户端; 客户端访问AIDLService; 10.3、电话管理器:...
Android 的 Remote Service 开发实例RemoteServiceClient~
实现了android的远程service调用哪个,使用了AIDL,并且提供了一个自定义类型的参数
android service 实例
第8章 Android中的后台服务Service 8.1 Service的作用 8.2 Service的实现 8.2.1 创建Service 8.2.2 启动Service 8.3 Toast和Notification应用 8.3.1 使用Notification通知 用户服务启动 8.3.2 使用Toast显示通知信息...
NULL 博文链接:https://javajeye.iteye.com/blog/803007
Android Service AIDL实例