android service组件学习-1
2012-03-08 19:28
239 查看
这里使用播放器播放音乐来做例子.直接上代码:
package org.example.service; import java.io.IOException; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.os.IBinder; import android.util.Log; public class MServices extends Service { //使用该类来播放音乐 private MediaPlayer player; /** * DOC 该方法在生命周期中只会调用一次 */ @Override public void onCreate() { if (player == null) { //实例化播放器 player=new MediaPlayer(); try { //设置指向的歌曲 player.setDataSource("/sdcard/1.mp3"); player.prepare(); } catch (Exception e) { e.printStackTrace(); } } super.onCreate(); } /** * DOC 该方法在生命周期中会被多次掉的用 * (使用activity.startService()方法) */ @Override public int onStartCommand(Intent intent, int flags, int startId) { Bundle extras = intent.getExtras(); if (extras != null) { //根据不同的请求来处理动作 int data = extras.getInt("data"); switch (data) { case 1: play(); break; case 2: pause(); break; case 3: stop(); break; default: break; } } return super.onStartCommand(intent, flags, startId); } private void stop() { if (player != null) { System.out.println("stop"); player.stop(); try { player.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } private void pause() { if (player != null && player.isPlaying()) { System.out.println("pause"); player.pause(); } } private void play() { if (player != null && !player.isPlaying()) { System.out.println("play"); player.start(); } } @Override public void onDestroy() { if (player != null) { player.stop(); player.release(); } super.onDestroy(); } @Override public IBinder onBind(Intent arg0) { return null; } }
package org.example.service; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ServicesActivity extends Activity implements OnClickListener { private Button play, pause, stop, close, exit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); play = (Button) findViewById(R.id.play); pause = (Button) findViewById(R.id.pause); stop = (Button) findViewById(R.id.stop); close = (Button) findViewById(R.id.close); exit = (Button) findViewById(R.id.exit); play.setOnClickListener(this); pause.setOnClickListener(this); stop.setOnClickListener(this); close.setOnClickListener(this); exit.setOnClickListener(this); } @Override public void onClick(View v) { //启用service Intent intent = new Intent(this, MServices.class); int i = 0; if (v.getId() == play.getId()) { i = 1; } else if (v.getId() == pause.getId()) { i = 2; } else if (v.getId() == stop.getId()) { i = 3; } else if (v.getId() == close.getId()) { stopService(intent); } else if (v.getId() == exit.getId()) { this.finish(); } if (i != 0) { intent.putExtra("data", i); startService(intent); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/play" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="play" /> <Button android:id="@+id/pause" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="pause" /> <Button android:id="@+id/stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="stop" /> <Button android:id="@+id/close" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="close" /> <Button android:id="@+id/exit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="exit" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.example.service" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".ServicesActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MServices"></service> </application> </manifest>
相关文章推荐
- Android 中 Service 学习--与组件的通讯5法
- Android重要组件之一 Service 服务讲解学习(一)
- Android学习——四大组件之Service
- Android重要组件之一 Service 服务讲解学习(一)
- Android 四大组件学习之Service七
- 【android学习】四大组件-Service
- 学习笔记:Android基本组件之Service
- Android四大组件之Service全面学习
- Android开发学习 组件Service的基础知识
- Intent启动系统组件(activity,service,BroadReceiver)-android学习之旅(四十九)
- android四大组件学习之-Service的使用
- Intent启动系统组件(activity,service,BroadReceiver)-android学习之旅(四十九)
- Android四大组件之Service服务学习笔记(一)
- Android学习日志11__四大组件02__service
- Intent启动系统组件(activity,service,BroadReceiver)-android学习之旅(四十九)
- Android 四大组件学习之Service六
- Android学习(十四) Service组件
- Android学习--四大组件之 Service(二)
- Android组件Service学习
- Android 四大组件学习之Service三