欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android基礎(chǔ)教程之開(kāi)機(jī)啟動(dòng)示例

 更新時(shí)間:2014年02月24日 16:26:25   作者:  
這篇文章主要介紹了android開(kāi)機(jī)啟動(dòng)示例,開(kāi)機(jī)自動(dòng)啟動(dòng)程序后開(kāi)機(jī)啟動(dòng)廣播功能實(shí)現(xiàn),需要的朋友可以參考下

Manifest.xml文件:

復(fù)制代碼 代碼如下:

<service
            android:name=".DaemonService"
            android:enabled="true"
            android:process=".DaemonService" >
            <intent-filter android:priority="1000">
                <action android:name="cn.test.DaemonService" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>

注釋:因?yàn)槲覀冞@個(gè)服務(wù)要一直在后臺(tái)運(yùn)行,所以不采用bindService的方式,而是直接采用startService的方式。

這樣就不至于我們的程序結(jié)束,也把我們的服務(wù)也結(jié)束掉了。

復(fù)制代碼 代碼如下:

package cn.start.test;

import java.util.List;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.Service;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class DaemonService extends Service {

    private static final String TAG = "Alarmreceiver";
    Handler hd1 = new Handler();
    int delay = 5000;

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

    @SuppressLint("NewApi")
    public void onCreate() {

        System.out.println("服務(wù)啟動(dòng)成功。。。。。。。。。。");
        hd1.postDelayed(mTasks, delay);

    }

    private Runnable mTasks = new Runnable() {
        @SuppressLint("NewApi")
        public void run() {
            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND);
            if (checkMainAppIsActive()) {
                Log.d(TAG, "服務(wù)檢測(cè)主應(yīng)用還在運(yùn)行");

            } else {
                Log.d(TAG, "服務(wù)檢測(cè)主應(yīng)用已經(jīng)關(guān)閉");
                Intent intent = getPackageManager().getLaunchIntentForPackage(
                        "cn.start.test");
                if (intent != null) {
                    DaemonService.this.startActivity(intent);
                    Log.d(TAG, "服務(wù)啟動(dòng)主應(yīng)用程序。");
                } else {
                    Log.d(TAG, "服務(wù)檢測(cè)到?jīng)]有安裝主應(yīng)用,自動(dòng)退出。");
                    return;
                }
            }
            hd1.postDelayed(mTasks, delay);

        }
    };

    /**
     * 檢測(cè)是否主程序是否還在運(yùn)行
     * @return
     */
    @SuppressLint({ "NewApi", "NewApi" })
    public boolean checkMainAppIsActive(){
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        //獲取正在運(yùn)行的應(yīng)用
        List<RunningAppProcessInfo> run = am.getRunningAppProcesses();
        for(RunningAppProcessInfo ra : run){
            if(ra.processName.equals("cn.start.test")){
                return true;
            }
        }
        return false;
    }
}

開(kāi)機(jī)自動(dòng)啟動(dòng)程序,自然少不了開(kāi)機(jī)啟動(dòng)廣播了。

manifest.xml 文件:

復(fù)制代碼 代碼如下:

<receiver android:name=".StartupReceiver" >
            <intent-filter android:priority="1000" >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

復(fù)制代碼 代碼如下:

public class StartupReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent i = new Intent(context,LoginActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

       
        Intent serviceIntent = new Intent(context,DaemonService.class);
        context.startService(serviceIntent);
    }
}

總結(jié):開(kāi)機(jī)啟動(dòng)廣播一定在manifest文件中注冊(cè)好。定時(shí)器要注意定時(shí)延遲。

通過(guò)List<RunningAppProcessInfo> run = am.getRunningAppProcesses();判斷自己的進(jìn)程是否還在運(yùn)行

通過(guò)getPackageManager().getLaunchIntentForPackage( "cn.start.test"); getPackageManager().getLaunchIntentForPackage( "cn.start.test");

來(lái)啟動(dòng)自己的程序。

相關(guān)文章

  • Android在layout xml中使用ViewStub完成動(dòng)態(tài)加載問(wèn)題

    Android在layout xml中使用ViewStub完成動(dòng)態(tài)加載問(wèn)題

    這篇文章主要介紹了Android在layout xml中使用ViewStub完成動(dòng)態(tài)加載問(wèn)題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-08-08
  • Android 程序申請(qǐng)權(quán)限注意事項(xiàng)

    Android 程序申請(qǐng)權(quán)限注意事項(xiàng)

    本主要介紹Android 程序申請(qǐng)權(quán)限注意事項(xiàng),這里整理了相關(guān)資料,并詳細(xì)說(shuō)明如何避免開(kāi)發(fā)的程序支持設(shè)備減少,有需要的小伙伴可以參考下
    2016-09-09
  • Flutter快速制作一個(gè)水印組件實(shí)例詳解

    Flutter快速制作一個(gè)水印組件實(shí)例詳解

    這篇文章主要為大家介紹了Flutter快速制作一個(gè)水印組件實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android仿手機(jī)通訊錄地址選擇功能

    Android仿手機(jī)通訊錄地址選擇功能

    這篇文章主要為大家詳細(xì)介紹了Android仿手機(jī)通訊錄地址選擇功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 深入理解Android中的xmlns:tools屬性

    深入理解Android中的xmlns:tools屬性

    關(guān)于xmlns:tools屬性的介紹網(wǎng)上有很多,小編覺(jué)得有必要整理一篇介紹較為詳細(xì)的內(nèi)容給大家,下面這篇文章就很深入的介紹了關(guān)于Android中的xmlns:tools屬性,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。
    2016-12-12
  • Android?Jetpack?組件LiveData源碼解析

    Android?Jetpack?組件LiveData源碼解析

    這篇文章主要為大家介紹了Android?Jetpack?組件LiveData源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Kotlin掛起函數(shù)應(yīng)用介紹

    Kotlin掛起函數(shù)應(yīng)用介紹

    掛起函數(shù)用狀態(tài)機(jī)以掛起點(diǎn)將協(xié)程的運(yùn)算邏輯拆分成不同的片段,每次執(zhí)行協(xié)程運(yùn)行不同的邏輯片段,由此可以知道協(xié)程是運(yùn)行在線程中的,線程的并發(fā)處理方式也可以用在協(xié)程上
    2022-11-11
  • Android Studio編寫AIDL文件后如何實(shí)現(xiàn)自動(dòng)編譯生成

    Android Studio編寫AIDL文件后如何實(shí)現(xiàn)自動(dòng)編譯生成

    這篇文章主要介紹了Android Studio編寫AIDL文件后如何實(shí)現(xiàn)自動(dòng)編譯生成,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • 利用Kotlin的協(xié)程實(shí)現(xiàn)簡(jiǎn)單的異步加載詳解

    利用Kotlin的協(xié)程實(shí)現(xiàn)簡(jiǎn)單的異步加載詳解

    這篇文章主要給大家介紹了關(guān)于利用Kotlin的協(xié)程實(shí)現(xiàn)簡(jiǎn)單的異步加載的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • 基于Android中手勢(shì)交互的實(shí)現(xiàn)方法

    基于Android中手勢(shì)交互的實(shí)現(xiàn)方法

    本篇文章是對(duì)Android中手勢(shì)交互的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下
    2013-05-05

最新評(píng)論