您的位置:首页 > 移动开发 > Android开发

Android学习:Activity启动Service

2013-09-05 22:39 316 查看
LocalService.java

package com.example.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class LocalService extends Service {

private static final String TAG = "LocalService";
private IBinder binder;
MediaPlayer mediaPlayer = null;
Uri uri = Uri.parse("/sdcard/music.mp3");
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i(TAG, "onCreate");

super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}

@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Log.i(TAG, "onStart");
//启动播放器
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this,uri);
mediaPlayer.start();
}
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i(TAG, "onDestroy");
super.onDestroy();
}

public class LocalBinder extends Binder{
//返回本地服务
LocalService getLocalService(){
return LocalService.this;
}
}

}


MainActivity.java
package com.example.service;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button)findViewById(R.id.startBtn);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("LocalService", "开启服务");
startCustomService();
}
});
}

public void startCustomService(){
Intent intent = new Intent(this, LocalService.class);
startService(intent);
}
}


AndroidManifest.xml
<?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="17" />
<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="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>
<service android:name="com.example.service.LocalService">

</service>
</application>

</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息