【Qzone】打不死的小強 殺不死的服務 開機自啓動服務


2015-4-26 23:22

有幸得到一張入場券,晚上去音樂類大廳看青春微夢想微電影開幕,同同是人,爲麼TA們領的是姨媽巾,而我領的是隻小兔子。。。
圖片 

好了,不說廢話了,上代碼吧。

如何讓APP隨手機開機啓動,然後自啓動界面或自啓動後臺服務呢?

那麼就要用到強大的廣播接收者了。 

還有要怎麼防止自己的APP不被應用管理器殺死呢?

那麼就要用到強大的服務了,而且還要用到兩個服務。

以下Demo代碼即可以實現以上兩個功能。

先來個步驟吧:

1、
新建一個類並繼承於BroadcastReceiver

2、在清單文件中註冊receiver

3、新建一個類並繼承於Service 

4、
在清單文件中註冊service

5、再新建一個類並繼承於Service 

6、
在清單文件中註冊service 

注意的是: 

兩個服務的內容一樣,區別只在銷燬onDestory方法中啓動另一個服務,這也是服務不被殺死的關鍵。

不說了,看代碼。

清單文件
--/AutoOpenApp/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
    package="com.devwang.autoopenapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <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=".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.devwang.autoopenapp.AutoOpenService" >
        </service>
        <service android:name="com.devwang.autoopenapp.AutoOpenServicer" >
        </service>

        <receiver android:name=".BootBroadcastReciver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

佈局文件
--/AutoOpenApp/res/layout/activity_main.xml
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.devwang.autoopenapp.MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textSize="60sp"
        android:text="開機自啓APP" />

</RelativeLayout>

主界面
--/AutoOpenApp/src/com/devwang/autoopenapp/MainActivity.java
package com.devwang.autoopenapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView text=(TextView) findViewById(R.id.tv);
        text.setText("這是一個自動啓動的例子!");
        
        System.out.println("Service oncreate");
        Intent i = new Intent(this,AutoOpenService.class);
        startService(i);
    }


}

廣播接收者
-- /AutoOpenApp/src/com/devwang/autoopenapp/BootBroadcastReciver.java
package com.devwang.autoopenapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

//開機啓動完成的  廣播接受者 在這裏啓動APP Activity
public class BootBroadcastReciver extends BroadcastReceiver {
static final String ACTION = "android.intent.action.BOOT_COMPLETED";

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(ACTION)) {
//自啓動界面
//Intent i = new Intent(context, MainActivity.class);
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//context.startActivity(i);
//自啓動服務
Intent i = new Intent(context, AutoOpenService.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(i);
}
}

}
 
兩個服務
第一個服務
--
/AutoOpenApp/src/com/devwang/autoopenapp/AutoOpenService.java 
package com.devwang.autoopenapp;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class AutoOpenService extends Service {

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();

System.out.println("Servicer ondestory");
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

System.out.println("Servicer ondestory");
Intent i = new Intent(this, AutoOpenServicer.class);
startService(i);
}

}

第二個服務
--
/AutoOpenApp/src/com/devwang/autoopenapp/AutoOpenServicer.java
package com.devwang.autoopenapp;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class AutoOpenServicer extends Service {

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("Service onCreate");
}

//守護進程 在次方法中 本服務會被銷燬 此時可以啓動新的服務
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("Service ondestory");
Intent i = new Intent(this,AutoOpenService.class);
startService(i);
}


}

OK  
 ||
 V
Gif

開機自啓動後臺服務Gif 

http://bruceanne-gif.stor.sinaapp.com/autoservice.gif

兩進程互相守護防被殺死與單進程比較Gif

http://bruceanne-gif.stor.sinaapp.com/doubleservice.gif
 

Gif
 A
 ||
OK
 
(源自it黑馬的視頻教程) 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章