2012-09-07 101 views
1

我的代碼自動啓動爲啓動設備的Android後不自動啓動:爲什麼服務執行

public class AutoStart extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { 
      SetAlarm(context); 
     }  

    } 

    public void SetAlarm(Context context) { 
     AlarmManager am = (AlarmManager) context 
       .getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(context, ServiceUpdateData.class); 
     // PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 
     PendingIntent pi = PendingIntent.getService(context, 0, i, 
       PendingIntent.FLAG_CANCEL_CURRENT); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.add(Calendar.SECOND, 30);// 
     int dbtime = 10; 

     int time = 1000 * 60 * dbtime; 
     am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
       time, pi); // Millisec * Second * Minute 
     Toast.makeText(context, "Service Started Again", Toast.LENGTH_LONG) 
       .show(); 
    } 
} 

我在androidmainifest.xml

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

爲什麼我的後服務不自動啓動加啓動設備android。 你能幫我嗎?

+0

,其Android版本,你的測試? – user936414

回答

1

不要忘記設置Intent-FilterBOOT_COMPLETED

像:

<receiver android:name=".AutoStart"> 
    <intent-filter> 
     <action 
      android:name ="android.intent.action.BOOT_COMPLETED"> 
     </action> 
    </intent-filter> 
</receiver> 
+0

如何設置意圖過濾器 – mum

+0

我將它添加到我的答案 –

1
<receiver android:name="yourpackageName.classname" 

     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     </intent-filter> 
    </receiver> 
相關問題