2011-04-27 150 views
0

我想在設備啓動時開始一項活動。Android - 無法在設備啓動時自動啓動活動?

Stranege的事情是,我的代碼在仿真器上工作,但不是在實際設備上。

我有一個名爲preferences.xml的文件,它有一個CheckBoxPreference來設置自動啓動和關閉。

這裏是我的代碼: -

public class AutoStartUp extends BroadcastReceiver 
{ 
private static final String TAG = "AutoStartUp"; 
public void onReceive(Context context, Intent intent) 
{ 
    String action = intent.getAction(); 
    if(action.equals(Intent.ACTION_BOOT_COMPLETED)) 
    { 
     SharedPreferences sp = context.getSharedPreferences("preferences", 0); 
     if(sp.getBoolean("autostartup", false)) 
     { 
      Log.e(TAG, "I AM HERE"); 
      Intent startupIntent = new Intent(context, CompleteTaskManager.class); 
      context.startActivity(startupIntent); 
     } 
    } 
} 
} 

這裏是我的這個接收器清單部分: -

<receiver android:name=".AutoStartUp" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
    </receiver> 

請幫助!

回答