2015-09-04 35 views
0

我做了一個應用程序,我寫服務檢測到無線網絡併發送數據到服務器,爲此我編寫了Alarm Manager和廣播接收器,然後通過報警管理器和廣播接收機呼叫服務onCreate方法。我的代碼如下:後臺服務線程運行在一些手機上,但有些手機不是

BootCompletedIntentReceiver類調用30秒的廣播接收器。

public class BootCompletedIntentReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     AlarmManager service = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
     Intent background = new Intent(context, AlarmReceiver.class); 
      PendingIntent pending = PendingIntent.getBroadcast(context, 0, background, 
       PendingIntent.FLAG_CANCEL_CURRENT); 
      Calendar cal = Calendar.getInstance(); 
      // start 30 seconds after boot completed 
      cal.add(Calendar.SECOND, 30); 
      // fetch every 30 seconds 
      // InexactRepeating allows Android to optimize the energy consumption 
      service.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
       cal.getTimeInMillis(),2000, pending); 


     } 
    } 

AlarmReceiver類

public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent background = new Intent(context, MyService.class); 
     context.startService(background); 
     String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(ns); 


      int icon = R.drawable.ic_launcher; 
      CharSequence tickerText = "Service Started"; 
      long when = System.currentTimeMillis(); 

      Notification notification = new Notification(icon,tickerText, when); 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 

      CharSequence contentTitle = "Service Start!"; 
      CharSequence contentText = ""; 
      Intent notificationIntent = new Intent(context,AlarmReceiver.class); 
      PendingIntent contentIntent = PendingIntent 
        .getActivity(context, 0, notificationIntent, 0); 

      notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

      mNotificationManager.notify(1, notification); 
      // Log.d("test", "Saving Data to File from Service."); 
    } 

} 

從AlarReceiver服務獲取呼叫,第一onCreate方法,然後OnstartCommand ..我寫線程檢測到無線網絡。這代碼是爲一些電話,但一些手機上運行服務從未開始。爲什麼會發生?

我的清單文件代碼如下:

<service android:name="com.edbeans.attendance.MyService" 
       android:enabled="true" 
       android:process=":my_process" 
       android:exported="true" 
       android:icon="@drawable/ic_launcher"></service> 

     <receiver android:name="com.edbeans.attendance.AlarmReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 
     <receiver android:name="com.edbeans.attendance.BootCompletedIntentReceiver" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 

這是爲什麼發生的該服務從一些電話呼叫獲取併成功運行,但永遠不會啓動一些電話服務..

+0

使用exactRepeating警報! –

+0

哪些部分尚未在某些手機上運行,​​還請告訴我們您獲得了哪個手機的結果以及哪部手機沒有結果 –

+0

@MuhammadBabar我已經在BootCompletedIntentReceiver類中使用過。 – Asmi

回答

1

該系統可以回去接收到警報後立即進入低功耗狀態。爲了確保您的Service運行,您需要使用喚醒鎖在BroadcastReceiverService之間進行協調。查看WakefulReceiver作爲幫助解決這個問題的一個選項。

+0

感謝vl現在檢查 – Asmi

+0

你能告訴我怎樣才能改變我的代碼 – Asmi

+0

這篇文章將有助於解釋警報和演示代碼有一個例子:http://hiqes.com/android-alarm-ins-outs/ –

相關問題