Android應用程式開機開機啟動

來源:互聯網
上載者:User

標籤:android   style   blog   color   os   檔案   io   art   

  有很過情況都需要Android程式開機自啟,也就是在手機開機之後馬上執行相應的Android程式。

  實現的方法就是,在手機開機的時候接受相應的廣播,在Android程式中接受相應的廣播。

  第1步:建立自訂的BroadcastReceiver類。

 1 package com.example.autostart; 2  3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.widget.Toast; 7  8 public class BootupReceiver extends BroadcastReceiver { 9 10     @Override11     public void onReceive(Context context, Intent intent) {12         Toast.makeText(context, "App started", Toast.LENGTH_LONG).show();13         // ---start the main activity of our app---14         15         Intent i = new Intent(context, MainActivity.class);16         i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);17         context.startActivity(i);18     }19 }

  第2步:修改AndroidManifest.xml檔案。

  添加收取到開機廣播的許可權。

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

  註冊自訂的BroadcastReceiver類。

1 <receiver android:name=".BootupReceiver" >2   <intent-filter>3     <action android:name="android.intent.action.BOOT_COMPLETED" />4 5     <category android:name="android.intent.category.DEFAULT" />6   </intent-filter>7 </receiver>

  這樣就基本完成了,我在電腦的AVD上實驗也成功了。但在我手機上開機沒有效果,我在手機管家中也設定了相應的許可權,但就是不成功。在我把手機上安全軟體卸載後,程式自啟動成功了,我分析是安裝的手機管家在接受開機廣播後,就廣播結束了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.