2013-04-09 34 views
0
public class AlertBootCompletedReceiver extends BroadcastReceiver { 

    private PackageManager pm; 
    private boolean isStoredExternally; 

    @Override 
    public void onReceive(Context context, Intent arg1) { 
     // TODO Auto-generated method stub 

     pm = context.getPackageManager(); 
     try { 
      PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0); 
      ApplicationInfo ai = pi.applicationInfo; 
      isStoredExternally = (ai.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE; 

     } catch (NameNotFoundException e) { 
      // do something 
     } 
     if (arg1.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 

      if (!isStoredExternally) { 
       // I start a service here 
      } 
     } 
     if (arg1.getAction().equals(
       Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE) 
       && isStoredExternally) { 

      // I start a service here .. 
     } 

    } 

} 

我通過接收ALERT_BOOT_COMPLETED從BroadCastReceiver啓動服務。此代碼適用於像三星SII這樣的手機,但不適用於索尼Xperia Neo等其他手機。任何人都可以告訴我該怎麼做。我有點卡住..ALERT_BOOT_COMPLETED在某些手機中沒有收到,但工作得很好,否則

+0

我已經在我的接收器中添加了清單和意圖篩選器操作中的所有權限... – 2013-04-09 22:09:05

回答

1

它可能是工作設備是前3.1的情況。如果我沒有記錯,在3.1之後,只有在用戶第一次打開應用程序之後才能處理廣播。

檢查this blog post

+0

我在接收者的intent-filter :: android.intent.action.BOOT_COMPLETED和android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE中使用了這兩個。此外,代碼適用於三星SII有v4.0.1,但不適用於索尼xperio NEO有v4.0.4和另一個設備是V2.3.5 ...我現在更加困惑..請幫助! – 2013-04-09 22:19:00

+0

我不知道我明白你的意思 – fedepaol 2013-04-09 22:21:33

+0

對不起,我輸入錯誤..請參閱最近的評論... – 2013-04-09 22:23:51

相關問題