2015-12-31 50 views
5

我添加我的後臺服務運行完美,但我的手機關閉,然後打開然後我的reciver類在獲取操作android.intent.action.BOOT_COMPLETED.but服務不start.my代碼請幫助我!BOOT_COMPLETED之後爲什麼不啓動服務?

public class ReceiverCall extends BroadcastReceiver { 
    static final String ACTION = "android.intent.action.BOOT_COMPLETED"; 
    static final String ACTION1 = "android.intent.action.QUICKBOOT_POWERON"; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.v("log_tag", "Action :: "+intent.getAction()); 
     if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { 
       /* Setting the alarm here */ 
       Intent alarmIntent = new Intent(context, MyAlarmService.class); 
       PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0); 
       AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
       Calendar calendar = Calendar.getInstance(); 
       calendar.setTimeInMillis(System.currentTimeMillis()); 
       manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), + (1000 * 60 * 2), pendingIntent); 
       Log.v("log_tag", "REPEAT"); 
      } 
    } 

而且我還在顯示中添加權限。

<uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="com.employeemanager.app.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.BROADCAST_STICKY" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.GET_TASKS" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

    <application 
     android:allowBackup="true" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="***8" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.NoTitleBar" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <service 
      android:name="***.MyAlarmService" 
      android:enabled="true" > 
     </service> 

     <receiver android:name="***.ReceiverCall" 
      android:enabled="true" > 
      <intent-filter> 
       <action android:name="***" /> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
       <action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" /> 
       <category android:name="android.intent.category.LAUNCHER" > 
       </category> 
      </intent-filter> 
     </receiver> 
    </application> 
+0

您的應用程序在SD卡上嗎?另外,當你把你的接收器和服務放在你的清單上時,試着把整個軟件包寫成:com.your.package.ReceiverCall並讓我知道它是否可用 –

+0

Skizo我也添加了完整的軟件包名稱,但沒有任何工作。 – crickpatel0024

+0

查看我的回答請 –

回答

3

也許你misstyped,但如果你想那Intent工作,你應該叫:

context.startService(alarmIntent); 

讓我知道,如果你的logcat中顯示此日誌Log.v("log_tag", "Action :: "+intent.getAction());請這一個Log.v("log_tag", "REPEAT");了。

我建議你BroadcastReceiver將是這樣的:

public class ReceiverCall extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    Log.v("log_tag", "Action :: "+intent.getAction()); 
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { 
      //make an intent to your Service as follows 
      Intent serviceIntent = new Intent(context, MyAlarmService.class); 
      context.startService(serviceIntent); 
     } 
} 

然後在你的Service在您ServiceonCreate補充一點:

/* Setting the alarm here */ 
Intent alarmIntent = new Intent(context, Whatever.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0); 
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), + (1000 * 60 * 2), pendingIntent); 
Log.v("log_tag", "REPEAT"); 

我也建議你創建一個新BroadcastReceiver並在其上做你想要重複的東西,並用BroadccastReceiver替換whatever.class

+0

12-31 17:00:48.645:V/log_tag(2473):Action :: android.intent.action.BOOT_COMPLETED 12-31 17:00:48.649:V/log_tag(2473):REPEAT – crickpatel0024

+0

您是否添加了' content.startService(alarmIntent);'? –

+0

一分鐘我檢查它。 – crickpatel0024

2

你的應用程序安裝在外接存儲一個偶然的機會,它不能接收啓動完成的事件。您應該在清單中添加android:installLocation="internalOnly"

+0

Helmi我添加內部但不工作..! – crickpatel0024

相關問題