2010-06-04 127 views
5

可能重複啓動設備的電源:
how to create startup application in android?
How to Autostart an Android Application?運行我的後臺應用程序,當我在Android中

嗨,

我想在我的一個應用程序,當我要開始我的意思是我的谷歌機器人g1的電源我的應用程序會自動啓動,但我無法解開derstand如何 我這樣做,請幫助......

+0

IIRC,android應用程序可以是「屏幕」或「服務」。你想要做的是在手機上安裝一項服務。向下搜索該路徑。 – KevinDTimm 2010-06-04 12:57:03

+0

它也是http://stackoverflow.com/questions/1056570/how-to-autostart-an-android-application – CommonsWare 2010-06-04 13:59:10

回答

8

這是我想你想要做的一個基本例子。你需要做一些事情。首先,授予您的應用程序權限以偵聽「引導完成」信號。在AndroidManifest.xml中,在清單中添加該行:

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

根據您的應用程序部分,添加和指定的廣播接收器爲這個目的:

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

您還需要一個廣播接收器將會響應這個意圖並在啓動時啓動您的服務。在您的項目中創建MyBroadcastReceiver.java:

package com.mypackage.myapp; 

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

public class MyBroadcastReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context aContext, Intent aIntent) { 

     // This is where you start your service 
     aContext.startService(new Intent(aContext, MyService.class)); 
    } 
} 
+0

的副本:方法startService(Intent)未定義類型MyBroadcastReceiver,它也強調MyService。 – eawedat 2013-10-30 12:08:45

相關問題