android应用开发详解(十七)---------------service初级
来源:互联网 发布:有答案中级java面试题 编辑:程序博客网 时间:2024/05/16 16:21
1、工程目录
2、MainActivity.java
package com.example.test_service;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;import android.app.Activity;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;public class MainActivity extends Activity {private Button startBtn, stopBtn, bindBtn, unbindBtn;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main);startBtn = (Button) findViewById(R.id.button01);stopBtn = (Button) findViewById(R.id.button02);bindBtn = (Button) findViewById(R.id.button03);unbindBtn = (Button) findViewById(R.id.button04);startBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setAction("com.example.test_service.action.MY_SERVICE");startService(intent);}});stopBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setAction("com.example.test_service.action.MY_SERVICE");stopService(intent);}});final ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName arg0) {// TODO Auto-generated method stubLog.i("SERVICE", "断开连接");Toast.makeText(MainActivity.this, "断开连接!", Toast.LENGTH_LONG).show();}@Overridepublic void onServiceConnected(ComponentName arg0, IBinder arg1) {// TODO Auto-generated method stubLog.i("SERVICE", "连接成功");Toast.makeText(MainActivity.this, "连接成功!", Toast.LENGTH_LONG).show();}};bindBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setAction("com.example.test_service.action.MY_SERVICE");bindService(intent, conn, Service.BIND_AUTO_CREATE);}});unbindBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setAction("com.example.test_service.action.MY_SERVICE");unbindService(conn);}});}}
3、MyService.java
package com.example.test_service;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{@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();Log.i("SERVICE", "on create.................");Toast.makeText(MyService.this,"on create .........." , Toast.LENGTH_LONG).show();}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();Log.i("SERVICE", "on destroy.................");Toast.makeText(MyService.this,"on destroy .........." , Toast.LENGTH_LONG).show();}@Override@Deprecatedpublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);Log.i("SERVICE", "on start.................");Toast.makeText(MyService.this,"on start .........." , Toast.LENGTH_LONG).show();}@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubLog.i("SERVICE", "on bind.................");Toast.makeText(MyService.this,"on bind .........." , Toast.LENGTH_LONG).show();return null;}}
4、main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动Service" /> <Button android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止Service" /> <Button android:id="@+id/button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绑定Service" /> <Button android:id="@+id/button04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解绑Service" /></LinearLayout>
5、Android Menifest.xml中添加Service的注册
<service android:name="MyService" > <intent-filter> <action android:name="com.example.test_service.action.MY_SERVICE" /> </intent-filter> </service>
6、打印输出
0 0
- android应用开发详解(十七)---------------service初级
- android应用开发详解(十七)-----------------------BroadcastReceiver
- android应用开发详解(十七)-------------------BroadcastReceiver(续)
- android开发(十七) requestWindowFeature详解
- Android Service开发详解
- android开发之service详解
- Android应用:Service开发实例(三)之Service的具体应用方法
- Android应用开发详解
- Android应用开发详解
- Android应用开发详解
- Android开发学习笔记秘籍(十七)
- Android产品研发(十七)-->Hybrid开发
- Android产品研发(十七)-->Hybrid开发
- Android应用开发基础篇(6)-----Service
- ros 开发要点(初级实现topic,service,message,launch)
- Android开发四大组件之Service(详解篇)
- Android开发四大组件之Service(详解篇)
- Android应用开发学习笔记之Service
- 常用的HTTP状态码
- HDU 1348 Wall 凸包的周长
- 动态添加删除ExpandableListView的item的例子
- Matlab中利用mex编译Opencv实现画板绘图功能
- ubuntu 14.04 安装mentohust
- android应用开发详解(十七)---------------service初级
- JSP登入界面(HTML+JavaBan)
- UVA 816 - Abbott's Revenge(BFS)
- Android_XmlPullParser解析xml文件_141014
- 2014省赛
- oc 数组常用函数随堂笔记
- 有上下界的网络流
- 女人的聚会
- pwm驱动接口编写