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>
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>
相关文章推荐
- Intent启动系统组件(activity,service,BroadReceiver)-android学习之旅(四十九)
- Intent启动系统组件(activity,service,BroadReceiver)-android学习之旅(四十九)
- Intent启动系统组件(activity,service,BroadReceiver)-android学习之旅(四十九)
- android 学习笔记 Activity 和service 的启动过程
- Intent启动系统组件(activity,service,BroadReceiver)-android学习之旅(四十九)
- 【Android】Activity切换效果——当通过Service启动自己Activity的时候怎么控制
- Android 7.0 ActivityManagerService(2) 启动Activity的过程:一
- Android service里面启动activity和alertdialog .
- android学习--Activity及应用启动过程
- android ActivityManagerService 学习 (一)
- Android 在Service里面启动Activity
- Android学习历程(一)——Activity的启动/关闭和内部的数据传递
- Android开发笔记----Activity再学习(生命周期、启动模式、intent-filter)
- Android ActivityManagerService(AMS)的启动分析
- Android基础学习之怎么在Activity启动时获得View的宽高
- Android学习小结之获取被启动的Activity传回的数据
- Android 7.0 ActivityManagerService(3) 启动Activity的过程:二
- Android 7.0 ActivityManagerService(4) 启动Activity的过程:三
- Android Service BroadcastReceiver 合用 应用全局随便启动activity
- Android学习之Activity与Service进行通信的三种方式