android,service实例,播放音乐

来源:互联网 发布:手机淘宝么注册网店 编辑:程序博客网 时间:2024/05/17 02:45
package com.example.servicedemo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button buttonStart, buttonStop;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);buttonStart = (Button) findViewById(R.id.buttonStart);buttonStop = (Button) findViewById(R.id.buttonStop);buttonStart.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO 自动生成的方法存根startService(new Intent(MainActivity.this, MyService.class));//startService(new Intent(this, MyService.class));//Log.i("--------------------buttonStart----------------------", "buttonStart");}});buttonStop.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO 自动生成的方法存根stopService(new Intent(MainActivity.this, MyService.class));//Log.i("--------------------buttonStop----------------------", "buttonStop");}});}// /onCreate}
package com.example.servicedemo;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.IBinder;import android.util.Log;import android.widget.Toast;/** * @author Administrator *  */public class MyService extends Service {MediaPlayer player;@Overridepublic IBinder onBind(Intent intent) {// TODO 自动生成的方法存根return null;}@Overridepublic void onCreate() {// TODO 自动生成的方法存根// super.onCreate();Toast.makeText(this, "My Service created", Toast.LENGTH_LONG).show();Log.i("----------------------onCreate----------------------", "onCreate");player = MediaPlayer.create(MyService.this, R.raw.chopstick_brother);player.setLooping(false);}@Overridepublic void onStart(Intent intent, int startId) {// TODO 自动生成的方法存根// /super.onStart(intent, startId);Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();Log.i("--------------------onStart----------------------", "onStart");player.start();}@Overridepublic void onDestroy() {// TODO 自动生成的方法存根//super.onDestroy();Toast.makeText(this, "My Service Stoped", Toast.LENGTH_LONG).show();Log.i("----------------------Stoped----------------------", "Stoped");player.stop();}}// / class MyService
<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="${relativePackage}.${activityClass}" >    <Button        android:id="@+id/buttonStart"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:layout_marginRight="107dp"        android:layout_marginTop="56dp"        android:text="start" />    <Button        android:id="@+id/buttonStop"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/buttonStart"        android:layout_alignBottom="@+id/buttonStart"        android:layout_alignParentRight="true"        android:layout_marginRight="18dp"        android:text="stop" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.servicedemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="15"        android:targetSdkVersion="16" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <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"            android:enabled="true" >        </service>    </application></manifest>

第一次:点击 start,输出 onCreate,onStart  音乐播放

第二次:点击stop,输出stoped 音乐停止播放

第三次:点击 start,输出 onCreate,onStart  音乐播放。前面已stop,需要重新创建 启动 

第三次:点击 start,输出 onStart  音乐播放,前面已启动,此时不需创建。此时继续播放,不重新播放。





1 0