service类:
package com.example.service; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.util.Log; import android.widget.Toast; /** * service也是运行在主线程,如果处理比较耗时的操作一样要另起新线程, * 没有startservice的情况下stopservice不会报错,没有onbindservice的情况下,onunbindservice会报错。 * bindservice需要serviceconnection对象关联。start不需要。 * 需要在配置文件中声明service。 * * @author Administrator * */ public class servicedemo extends Service { private MediaPlayer mediaPlayer = null; private static final String TAG = "MusicService"; //oncreate-onstart-ondestroy //oncreate-onbind-onunbind-ondestroy @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub Toast.makeText(servicedemo.this, "onBind()", Toast.LENGTH_LONG).show(); Log.i(TAG, "onBind()"); mediaPlayer.start(); return null; } @Override public void onCreate() { // TODO Auto-generated method stub Toast.makeText(servicedemo.this, "onCreate()", Toast.LENGTH_LONG).show(); Log.i(TAG, "onCreate()"); mediaPlayer=MediaPlayer.create(servicedemo.this, R.raw.huranzhijian); //在res文件夹下新建raw文件夹,放入一个mp3文件作为播放文件。 } @Override public void onDestroy() { // TODO Auto-generated method stub Toast.makeText(servicedemo.this, "onDestroy()", Toast.LENGTH_LONG).show(); Log.i(TAG, "onDestroy()"); mediaPlayer.stop(); } @Override public void onRebind(Intent intent) { // TODO Auto-generated method stub Toast.makeText(servicedemo.this, "onRebind()", Toast.LENGTH_LONG).show(); Log.i(TAG, "onRebind()"); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub Toast.makeText(servicedemo.this, "onStart()", Toast.LENGTH_LONG).show(); Log.i(TAG, "onStart()"); mediaPlayer.start(); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub Toast.makeText(servicedemo.this, "onUnbind()", Toast.LENGTH_LONG).show(); Log.i(TAG, "onUnbind()"); return false; } }
主Activity:
package com.example.service; import android.app.Activity; import android.content.ComponentName; import android.content.Context; 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; public class MainActivity extends Activity { private Button start = null; private Button stop = null; private Button onbind = null; private Button unbind = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button) findViewById(R.id.service_start); stop = (Button) findViewById(R.id.service_stop); onbind = (Button) findViewById(R.id.service_onbind); unbind = (Button) findViewById(R.id.service_unbind); start.setOnClickListener(clickListener); stop.setOnClickListener(clickListener); onbind.setOnClickListener(clickListener); unbind.setOnClickListener(clickListener); } /** * ServiceConnection */ final ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub // 解除链接时调用 } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub // 绑定链接时调用 } }; android.view.View.OnClickListener clickListener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, com.example.service.servicedemo.class); switch (v.getId()) { case R.id.service_start: startService(intent); break; case R.id.service_stop: stopService(intent); break; case R.id.service_onbind: bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); break; case R.id.service_unbind: unbindService(serviceConnection); } } }; }
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:id="@+id/service_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="22dp" android:layout_marginTop="35dp" android:text="start" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/service_start" android:text="service使用" android:textAppearance="?android:attr/textAppearanceMedium" /> <Button android:id="@+id/service_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/service_start" android:layout_alignBottom="@+id/service_start" android:layout_alignLeft="@+id/textView1" android:layout_marginLeft="20dp" android:text="stop" /> <Button android:id="@+id/service_onbind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/service_stop" android:layout_alignBottom="@+id/service_stop" android:layout_toRightOf="@+id/textView1" android:text="onbind" /> <Button android:id="@+id/service_unbind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/service_onbind" android:layout_alignBottom="@+id/service_onbind" android:layout_alignParentRight="true" android:text="unbind" /> </RelativeLayout>
配置文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.service" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <service android:name="com.example.service.servicedemo" > </service> <activity android:name="com.example.service.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> </application> </manifest>
相关推荐
android service 简单实例代码 . binder继承一个接口,实现方法。
Demo android service toast 第一个版本
android service使用的小demo 包括startService stopService bindService unbindService 两种开启、关闭service的小demo
android service简单代码编写 包括详细的注释信息
Android Service下载文件
关于Android Service的简单实例:属于start service类型,而不是bound service。
Android Service下载,EventBus更新界面,取代传统的用Handler或者广播更新界面,代码更加优雅,更加简洁
Android Service被关闭后自动重启,解决被异常kill 服务
android service 学习(下) ,共两部分
Android Service使用,包括Start Service和bind Service启动方式。Service与Acvitity互相传递数据
android Service中onStartCommand返回值.txt 有四种返回值,分别代表的含义
这是Pro Android学习系列中Android Service部分的例子源代码。相关学习笔记见:http://blog.csdn.net/flowingflying/article/details/6212512
通过Messenger实现Android Service更新UI
主要介绍了Android Service 服务不被杀死的妙招的相关资料,需要的朋友可以参考下
Android Service Demo
android aidl service
android service 源码 学习
android service 关于android中service的简单例子 适合初学者