關於android程式開機自啟動失敗的問題

來源:互聯網
上載者:User

因為公司需求,查了下android開機自啟動的代碼實現,使用的是部落格園-空山不空的代碼

然後發現開機啟動失敗,查了很久沒找到原因,網上說的方法比較統一,然後發現了他這

段代碼和別人的不同

      <SPAN style="COLOR: #ff00ff"><receiver android:name=".BootBroadcastReceiver"

        <intent-filter> 
        <action android:name="android.intent.action.BOOT_COMPLETED"
/> 
        <category android:name="android.intent.category.HOME"
/>
        </intent-filter> 
    </receiver>  </SPAN>  別人都沒有<span></span>,然後我將上段代碼修改如下 <receiver android:name=".BootBroadcastReceiver"

        <intent-filter> 
        <action android:name="android.intent.action.BOOT_COMPLETED"
/> 
        <category android:name="android.intent.category.HOME"
/>
        </intent-filter> 
    </receiver> 順便把許可權的那個也去了,開機啟動成功 附空山不空的完整方法:

背景知識:當Android啟動時,會發出一個系統廣播,內容為ACTION_BOOT_COMPLETED,它的字
符串常量表示為 android.intent.action.BOOT_COMPLETED。只要在程式中“捕捉”到這個訊息,再啟動之
即可。記住,Android架構說:Don''t call me, I''ll call you back。我們要做的是做好接收這個訊息的準備,而
實現的手段就是實現一個BroadcastReceiver。

1、介面Activity,BootStartDemo.java檔案

 

?
1234567891011121314151617181920212223242526 public
class
BootStartDemo extends
Activity {     /** Called when the activity is first created. */    @Override    public
void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 無title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        new
Thread() {             public
void run() {                 try
                    /*  8秒後關閉頁面*/                    sleep(10000);
                }
catch (Exception e) {
                    e.printStackTrace();
                }
finally {                     finish();
// 關閉頁面                 }
            }
        }.start();
      }
}

這段代碼很簡單,當Activity 啟動時,會顯示TextView,用它顯示你想顯示的字樣,並且這個頁面只顯示10秒後消失。

2、接收廣播訊息:BootBroadcastReceiver.java

?
1234567891011121314 public
class
BootBroadcastReceiver extends
BroadcastReceiver {     static
final String action_boot="android.intent.action.BOOT_COMPLETED"
      @Override    public
void onReceive(Context context, Intent intent) {
        if
(intent.getAction().equals(action_boot)){              Intent ootStartIntent=new
Intent(context,BootStartDemo.class); 
            ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            context.startActivity(ootStartIntent); 
        }
      }
  }

該類繼續自 BroadcastReceiver,覆載方法 onReceive 中,檢測接收到的 Intent 是否符合
BOOT_COMPLETED,如果符合,則啟動BootStartDemo這個Activity。

3、設定檔

(1)AndroidManifest.xml :

?
1234567891011121314151617181920212223 <?xml version="1.0"
encoding="utf-8"?>
<!-- 這是一個開機自啟動程式 -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.ajie.bootstartdemo"      android:versionCode="1"      android:versionName="1.0">
    <application android:icon="@drawable/icon"
android:label="@string/app_name">
        <activity android:name=".BootStartDemo"                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"
/>                 <category android:name="android.intent.category.LAUNCHER"
/>             </intent-filter>
        </activity>
    <SPAN style="COLOR: #ff00ff"><receiver android:name=".BootBroadcastReceiver"
        <intent-filter> 
        <action android:name="android.intent.action.BOOT_COMPLETED"
/>          <category android:name="android.intent.category.HOME"
/>         </intent-filter> 
    </receiver> 
</SPAN>    </application>
<SPAN style="COLOR: #ff00ff"><STRONG><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission></STRONG> 
</SPAN></manifest>

注意其中顏色標紅那一部分,該節點向系統註冊了一個 receiver,子節點 intent-filter 表示接收
android.intent.action.BOOT_COMPLETED 訊息。並且還要配置android.permission.RECEIVE_BOOT_COMPLETED許可權。

(2)Layout檔案,main.xml

?
1234567891011121314151617 <?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"          >
<TextView       android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:text="@string/boottext"    android:textColor="#5F2DD2"    android:background="#FFFFFF"    android:textSize="60px"     android:gravity="center_horizontal"    />
</LinearLayout>

完成後,編譯出apk包,安裝到模擬器或手機中。關機,重新開機,就會顯示BootStartDemo這個Activity顯示出來的頁面

相關文章

聯繫我們

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