android自啓動服務,研究自用

一、開機自啓動 二、系統服務(須要用到AIDL進行數據傳輸);拿到需求的第一步準備下手的是開機自啓動。java

1、開機自啓動android

這裏要分三步走:服務器

第一步:加入開機自啓動權限app

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>ide

第二步:註冊廣播接收器this

<receiver android:name=".MyReceiver">
       <intent-filter android:priority="4000">
      <action android:name="android.intent.action.BOOT_COMPLETED" />
      <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>spa

android:priority是啓動優先級,值越大優先級越高,因此這裏根據本身的狀況而定。code

MyReceiver源碼xml

public class MyReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
        	Log.i(StbService.TAG, "boot receiver"); 
            Intent i = new Intent(context, StbService.class);
            context.startService(i);
        }
    }
}

第三步:將當前當前應用生成的apk複製到system/app下,若是含有.so文件,須要複製到system/lib目錄下。有不少平臺開機自啓動不須要將應用複製到system/app下,直接安裝就能夠,blog

 

這個具體緣由不清楚。

以上三步就是實現開機自啓動的步驟。

2、搭建系統服務一個完整的系統服務

須要分爲通訊AIDL、服務器端、客戶端

一、建立你要傳遞數據的AIDL文件

AIDL是android接口定義語言,經常使用於進程間通訊,它是一種輕量級通訊語言,它能傳遞基本的數據類型或者帶基本數據類型的List,其餘狀況的類都須要經過

序列化。這裏要注意,服務器端和客戶端的AIDL文件必須同樣,且必須在相同的包名下。

代碼以下:

package com.konka.qosmonloader.aidl;
 
interface IStbParmService{
	String getStbParameter(String parmName);
}

上面的aidl會生成相應的Binder,由於sub類實現了Binder接口,因此之後會使用這個類。

二、建立服務器端代碼StbService

Stbservice.java實現以下:

public class StbService extends Service {
	public static final String TAG = "StbService";
	private String Account = "ServerID";
	public IBinder onBind(Intent intent) {
        return binder;
    }
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i(TAG, "StbService start");
		return super.onStartCommand(intent, flags, startId);
	}
	private final IStbParmService.Stub binder = new IStbParmService.Stub() {
        public static final String TAG = "IRemoteService.Stub";
 
		@Override
		public String getStbParameter(String parmName) throws RemoteException {
			String parmValue = "";
			if ("Account".equals(parmName)) {
				parmValue = Account; // 業務帳號。
			}
			return parmValue;
		}
    };
}

對應的服務器端清單文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.konka.qosmonloader"
    android:versionCode="1"
    android:versionName="1.0">
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <permission android:name="thomas.permission.AIDL_SERVICE"/> 
	<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.konka.qosmonloader.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.konka.qosmonloader.StbService"
            android:label="StbService"
            android:enabled="true"
            android:exported="true"
            android:process=":remote"
			android:permission="thomas.permission.AIDL_SERVICE">
            <intent-filter>
            	<action android:name="com.certus.ottstb.bestv.StbParmService"/>
        	</intent-filter>
        </service>
        <receiver android:name=".MyReceiver">
	        <intent-filter android:priority="4000">
		       <action android:name="android.intent.action.BOOT_COMPLETED" />
		       <category android:name="android.intent.category.DEFAULT" />
		    </intent-filter>
	    </receiver>
    </application>
</manifest>

特別注意:一、須要加入權限<permission android:name="thomas.permission.AIDL_SERVICE"/> 二、自定義命名一個action,供客戶端訪問

 

我這裏命名爲<action android:name="com.certus.ottstb.bestv.StbParmService"/>這個名字能夠隨便命名,可是客戶端過濾的

action必須同名。

服務器端的目錄結構以下:

三、建立客戶端Activity

目錄結果以下:

MainActivity.java代碼

public class MainActivity extends Activity {
	private IStbParmService myService;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Intent intent=new Intent("com.certus.ottstb.bestv.StbParmService");  
		this.getApplicationContext().bindService(intent, conn, BIND_AUTO_CREATE);
	}
	private ServiceConnection conn=new ServiceConnection() {  
        
        @Override  
        public void onServiceDisconnected(ComponentName name) {  
            // TODO Auto-generated method stub  
            myService=null;  
        }  
        @Override  
        public void onServiceConnected(ComponentName name, IBinder service) {  
            // TODO Auto-generated method stub  
                myService=IStbParmService.Stub.asInterface(service);
                try {
					String a = myService.getStbParameter("Account");
					Toast.makeText(MainActivity.this, a, Toast.LENGTH_LONG).show();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        }  
    };  
}

這裏須要注意:註冊用的action要跟前面提到的本身命名的action同樣

接下來是清單文件的代碼:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.aidltest"
    android:versionCode="1"
    android:versionName="1.0">
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
	<uses-permission android:name="thomas.permission.AIDL_SERVICE"/>  
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.aidltest.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>
    </application>
</manifest>

一樣也要加入權限<uses-permission android:name="thomas.permission.AIDL_SERVICE"/> 

相關文章
相關標籤/搜索