Android 開機自動啟動服務

來源:互聯網
上載者:User

在前面的文章中提到了remote service 的建立過程,現在我們要讓它開機自動啟動

 

1.在前面代碼的基礎上添加 RemoteServiceBootReceiver.java ,實現一個intent的receiver

package com.fly;<br />import android.content.BroadcastReceiver;<br />import android.content.Context;<br />import android.content.Intent;<br />import android.util.Log;<br />public class RemoteServiceBootReceiver extends BroadcastReceiver {<br />private static final String TAG = "U0fly RemoteServiceBootReceiver";<br />static final String ACTION = "android.intent.action.BOOT_COMPLETED";<br />@Override<br />public void onReceive(Context arg0, Intent arg1) {<br />Log.d(TAG, "Boot completed");<br />// TODO Auto-generated method stub<br />if (arg1.getAction().equals(ACTION)) {<br />// service<br />Intent myintent = new Intent(arg0, RemoteService.class);<br />myintent.setAction("com.fly.RemoteService");<br />arg0.startService(myintent);<br />}<br />}<br />}<br />

2.在AndroidManifast.xml中添加許可權,並註冊一個receiver

<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br />package="com.fly" android:versionCode="1" android:versionName="1.0"><br /><application android:icon="@drawable/icon" android:label="@string/app_name"><br /><activity android:name=".RemoteServiceActivity"<br />android:label="@string/app_name"><br /><intent-filter><br /><action android:name="android.intent.action.MAIN" /><br /><category android:name="android.intent.category.LAUNCHER" /><br /></intent-filter><br /></activity><br /><service android:name="RemoteService"><br /><intent-fliter><br /><action android:name="com.fly.RemoteService" /><br /></intent-fliter><br /></service></p><p><receiver android:name=".RemoteServiceBootReceiver"><br /><intent-filter><br /><action android:name="android.intent.action.BOOT_COMPLETED" /><br /></intent-filter><br /></receiver></p><p></application></p><p><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission><br /><uses-sdk android:minSdkVersion="7" /><br /></manifest>

 

 

3.下面是一個簡單一實例, 完成開機啟動Activity的功能:

運行效果:

代碼:

 

 

BootSayHello.java

package com.fly;<br />import android.app.Activity;<br />import android.os.Bundle;<br />public class BootSayHello extends Activity {<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> }<br />}

BootBroadcastReceiver.java

package com.fly;<br />import android.content.BroadcastReceiver;<br />import android.content.Context;<br />import android.content.Intent;<br />public class BootBroadcastReceiver extends BroadcastReceiver {<br />static final String ACTION = "android.intent.action.BOOT_COMPLETED";<br />@Override<br />public void onReceive(Context context, Intent intent) {<br />// TODO Auto-generated method stub<br />if (intent.getAction().equals(ACTION)) {<br />Intent sayHelloIntent = new Intent(context, BootSayHello.class);<br />sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br />context.startActivity(sayHelloIntent);<br />}<br />}<br />}<br />

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br />package="com.fly" android:versionCode="1" android:versionName="1.0"><br /><application android:icon="@drawable/icon" android:label="@string/app_name"><br /><activity android:name=".BootSayHello" android:label="@string/app_name"><br /><intent-filter><br /><action android:name="android.intent.action.MAIN" /><br /><category android:name="android.intent.category.LAUNCHER" /><br /></intent-filter><br /></activity><br /><receiver android:name=".BootBroadcastReceiver"><br /><intent-filter><br /><action android:name="android.intent.action.BOOT_COMPLETED" /><br /></intent-filter><br /></receiver><br /></application><br /><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission><br /><uses-sdk android:minSdkVersion="7" /><br /></manifest>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.