Android 启动后台运行程序(Service)
2014-07-28 10:17
176 查看
Android开发中,当需要创建在后台运行的程序的时候,就要使用到Service。Service 可以分为有无限生命和有限生命两种。特别需要注意的是Service跟Activities是不同的(简单来说可以理解为后台与前台的区别),例如,如果需要使用Service的话,需要调用startService(),从而利用startService()去调用Service中的OnCreate()和onStart()方法来启动一个后台的Service。
启动一个Service的过程如下:context.startService() ->onCreate()- >onStart()->Service running其中onCreate()可以进行一些服务的初始化工作,onStart()则启动服务。
停止一个Service的过程如下:context.stopService() | ->onDestroy() ->Service stop。
接下来的实例是一个利用后台服务播放音乐的小例子,点击start运行服务,点击stop停止服务。
ServicesDemo.java(是一个Activity)
[java]
view plaincopy
package com.android.myservice;
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 ServiceDemo extends Activity implements OnClickListener {
private static final String TAG = "ServiceDemo";
Button buttonStart, buttonStop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
Log.i(TAG, "onClick: starting service");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
Log.i(TAG, "onClick: stopping service");
stopService(new Intent(this, MyService.class));
break;
}
}
}
除此之外还要在Manifest里面声明服务:(AndroidManifest.xml)
[html]
view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.myservice">
<application android:label="@string/app_name">
<activity android:name=".ServiceDemo" 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"/>
</application>
</manifest>
定义Service(MyService.java)
[java]
view plaincopy
package com.android.myservice;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service created", Toast.LENGTH_LONG).show();
Log.i(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.braincandy);
player.setLooping(false);
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stoped", Toast.LENGTH_LONG).show();
Log.i(TAG, "onDestroy");
player.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Start", Toast.LENGTH_LONG).show();
Log.i(TAG, "onStart");
player.start();
}
}
layout文件夹中是main.xml
[html]
view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/services_demo" android:gravity="center" android:textSize="20sp" android:padding="20dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonStart" android:text="@string/start"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" android:id="@+id/buttonStop"></Button>
</LinearLayout>
values 文件夹中是strings.xml
[html]
view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="services_demo">Service Demo</string>
</resources>
启动一个Service的过程如下:context.startService() ->onCreate()- >onStart()->Service running其中onCreate()可以进行一些服务的初始化工作,onStart()则启动服务。
停止一个Service的过程如下:context.stopService() | ->onDestroy() ->Service stop。
接下来的实例是一个利用后台服务播放音乐的小例子,点击start运行服务,点击stop停止服务。
ServicesDemo.java(是一个Activity)
[java]
view plaincopy
package com.android.myservice;
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 ServiceDemo extends Activity implements OnClickListener {
private static final String TAG = "ServiceDemo";
Button buttonStart, buttonStop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
Log.i(TAG, "onClick: starting service");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
Log.i(TAG, "onClick: stopping service");
stopService(new Intent(this, MyService.class));
break;
}
}
}
除此之外还要在Manifest里面声明服务:(AndroidManifest.xml)
[html]
view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.myservice">
<application android:label="@string/app_name">
<activity android:name=".ServiceDemo" 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"/>
</application>
</manifest>
定义Service(MyService.java)
[java]
view plaincopy
package com.android.myservice;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service created", Toast.LENGTH_LONG).show();
Log.i(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.braincandy);
player.setLooping(false);
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stoped", Toast.LENGTH_LONG).show();
Log.i(TAG, "onDestroy");
player.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Start", Toast.LENGTH_LONG).show();
Log.i(TAG, "onStart");
player.start();
}
}
layout文件夹中是main.xml
[html]
view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/services_demo" android:gravity="center" android:textSize="20sp" android:padding="20dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonStart" android:text="@string/start"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" android:id="@+id/buttonStop"></Button>
</LinearLayout>
values 文件夹中是strings.xml
[html]
view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="services_demo">Service Demo</string>
</resources>
相关文章推荐
- Android 启动后台运行程序(Service)
- Android 启动后台运行程序(Service)
- android 程序后台运行,定时刷新,像qq那样,即使程序不启动也照样运行
- android 程序强制停止后服务后台启动运行 ---- 不同程序间相助 使用第三方服务
- Android实现简单音乐播放器(startService和bindService后台运行程序)
- 怎样在android系统启动时自动运行自己的程序
- 让Java程序作为linux的Daemon后台运行 和 使用Java Service Wrapper将java程序作为linux服务并且开机自动启动
- Android原理 不需要关闭后台运行程序
- Android原理 不需要关闭后台运行程序
- Android之所以不需要关闭后台运行程序 的 理由
- android,wp7后台程序运行区别
- android bindService无法启动或不运行的问题
- android怎么判断程序在后台运行,让后台将程序图标显示在状态栏上?
- Android原理 不需要关闭后台运行程序
- symbian 安装后隐藏程序及启动后自动后台运行
- 让控制台程序后台运行并开机启动的简单方法
- android实现程序的后台运行
- Android原理 不需要关闭后台运行程序
- Android原理 不需要关闭后台运行程序
- 三十九、Android原理 不需要关闭后台运行程序