android 之 Service 播放音乐
2012-03-27 14:27
176 查看
Android里的Service 和 windows 里的Service功能差不多,就是一个不可见的进程在后台执行,避免被用户误关闭。因为android会在一定情况下关闭 未在前台显示的Activity, 所以如果要让一个功能一直在后台工作,不被系统关闭,就得用Service。比如闹钟,后台播放音乐。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ethan.service" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".ServiceDemoActivity" 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"></service> </application> </manifest>
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center" android:text="music" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center" android:text="stop" /> </LinearLayout>
package com.ethan.service; import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ServiceDemoActivity extends Activity implements OnClickListener { private static final String TAG = "ServicesDemo"; private Button buttonStart, buttonStop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonStart = (Button) findViewById(R.id.button1); buttonStop = (Button) findViewById(R.id.button2); buttonStart.setOnClickListener(this); buttonStop.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: Log.d(TAG, "onClick: starting srvice"); startService(new Intent(this, MyService.class)); break; case R.id.button2: Log.d(TAG, "onClick: stopping srvice"); stopService(new Intent(this, MyService.class)); break; } } }
相关文章推荐
- Android应用实例之----基于Service与ContentProvider的音乐播放实例!
- Android Service使用方法--简单音乐播放实例
- 简单音乐播放实例的实现,Android Service AIDL 远程调用服务
- Android MediaPlayer播放音乐有必要使用Service来调用吗?
- android在service中播放音乐
- Android通过Service实现音乐后台播放
- android音乐后台播放需要用到Service
- android实现开机自动播放音乐实例(Broadcast、Service)
- Android Service使用方法--简单音乐播放实例
- Android应用实例之----基于Service与ContentProvider的音乐播放实例!
- android 利用 service 实现音乐播放
- Android Service播放音乐
- android,service实例,播放音乐
- Android 音乐播放器的开发教程(六)service的运用及音乐列表点击播放 ----- 小达
- Android 超简单音乐播放器(六)播放暂停音乐,更换播放模式,上下歌曲切换(service)
- android 利用service来播放音乐
- android 音乐播放service
- Android Service使用方法--简单音乐播放实例
- 深入剖析Android四大组件(六)——相对完美的后台Service实现播放音乐功能
- Android基于Service服务的音乐播放