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

android开发教程之开机启动服务service示例

2016-03-12 22:27 483 查看
个例子实现的功能是:
1,安装程序后看的一个Activity程序界面,里面有个按钮,点击按钮就会启动一个Service服务,此时在设置程序管理里面会看的有个Activity和一个Service服务运行
2,如果手机关机重启,会触发你的程序里面的Service服务,当然,手机启动后是看不到你的程序界面。好比手机里面自带的闹钟功能,手机重启看不到闹钟设置界面
只是启动服务,时间到了,闹钟就好响铃提醒。

程序代码是:

首先要有一个用于开机启动的Activity,给你们的按钮设置OnClickListener();

public class MainActivity extends Activity {
private Button btnstarted = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnstarted = (Button)findViewById( R.id.btnstarted);
btnstarted.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,StartService.class);
startService(intent);
Toast.makeText(MainActivity.this, "服务启动成功", Toast.LENGTH_LONG).show();
}};}
}


  

我们要编写一个BroadcastReceiver用以捕获ACTION_BOOT_COMPLETED这条广播,并在捕获之后启动我们要启动的服务StarServie.class

public class BootCompletedReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent newIntent = new Intent(context,StartService.class);
context.startService(newIntent);
}
}
}


  

启动服务Service代码

public class StartService extends Service{
//public static String PHONENO;

public class LocalBinder extends Binder{
StartThief getService(){
return StartService.this;
}
}
public IBinder onBind(Intent intent){
return mBinder;
}
private void registerIntentReceiver(){
//此处添加启动服务要执行的操作代码
}
public void onStart(Intent intent,int startId){
super.onStart(intent, startId);
}
@Override
public void onCreate() {
registerIntentReceiver();
}
}


  

用到的Main.xml,里面只有一个Button ,id是btnstarted

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/AbsoluteLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:layout_height="wrap_content"
android:id="@+id/btnstarted"
android:text="@string/started"
android:layout_y="118dip"
android:layout_width="wrap_content"
android:layout_x="56dip">
</Button>
</AbsoluteLayout>


在AndroidManifest.xml配置文件中注册我们的BroadcastReceiver和服务Service

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thief" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<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=".StartService"></service>
//为了获取开机启动这个动作,必须注册加上android.intent.action.BOOT_COMPLETED
<receiver android:name=".BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>
获取开机启动动作的权限permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>"
</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: