2013-07-02 53 views
0

上下文:我試圖通過AlarmManager實現一個服務調用第二個服務(第一個子類)。爲此,我需要創建一個廣播接收器作爲內部類,如此處推薦的(Service and a BroadCastReceiver)。但是,看起來第一個服務永遠不會成功地調用第二個服務,或者第二個服務沒有收到該呼叫。在服務中調用AlarmManager

我環顧四周,發現其他人有類似的問題,從未解決:AlarmManager from service

在第一個服務調用第二的代碼很簡單:

private final long ONE_MINUTE = 60*1000*1; 

Context theContext = getBaseContext(); 
    AlarmManager theService = (AlarmManager) theContext 
      .getSystemService(Context.ALARM_SERVICE); 

    Intent i = new Intent(theContext, LocalWordSubClass.class); 

    PendingIntent thePending = PendingIntent.getBroadcast(theContext, 0, i, 
      PendingIntent.FLAG_CANCEL_CURRENT); 

    //call this after two minutes 
    theService.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+TWO_MINUTES, thePending); 

我的第二項服務是:

public class LocalWordSubClass extends LocalWordService { 
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if(checkIfGooglePlay() && checkTime()) { 
      getPostLocation(); 
     } 
     mLocationClient.disconnect(); //connected in the first service class 
     stopSelf(); 
    } 
}; 

}

我的清單與識別的第二服務:

<service 
     android:name=".LocalWordSubClass" 
     android:label="LocalWordSubClass" > 
    </service> 

問題:我沒有實現一個方法嗎?或者我是否需要將廣播接收器添加到清單中?對於第二班爲什麼沒有迴應,我有點困惑。

**更新:**感謝CommonWare的多項建議,我能夠在連接LocationClient和獲取其最後位置之間達到這一分鐘的時間。但是,當MainActivity的AlarmManager的setRepeating方法調用時,該服務不再重複運行。

這是代碼(即成功給locationClient時間來連接)

if(mLocationClient==null) { 
    pm = (PowerManager) getBaseContext().getSystemService(Context.POWER_SERVICE); 
    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); 
    wl.acquire(); 
     initalizeLocks(); 
     mLocationClient = new LocationClient(this, this, this); 
    mLocationClient.connect(); 
     Context theContext = getBaseContext(); 

    AlarmManager theService = (AlarmManager) theContext 
      .getSystemService(Context.ALARM_SERVICE); 

    Intent i = new Intent(LocalWordService.this, this.getClass()); 

    PendingIntent thePending = PendingIntent.getService(this, 0, i, 
      PendingIntent.FLAG_CANCEL_CURRENT); 
    //call this after two minutes 
    theService.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ONE_MINUTE, thePending); 
    } else { 
     getPostLocation(); 
     wl.release(); 
     mLocationClient = null; 
     stopSelf(); 
    } 

在MainActivity的AlarmManager是:

Calendar cal = Calendar.getInstance(); 

    Intent intent = new Intent(this, LocalWordService.class); 
    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0); 
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

    alarm. 
    setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), EVERY_TWO_MINUTES, pintent); 

回答

1

上午我未能實現的方法?

那麼,LocalWordSubClass好像沒有方法,除了繼承的方法。

另外,您正在嘗試將廣播Intent發送到Service(通過您的PendingIntent),這不會起作用。

而且,您在LocalWordSubClass中定義的BroadcastReceiver未被使用。

接下來,your related comment

我最初的服務調用一個兩分鐘的時間後,新的子類(它可以訪問LocationClient)

你的「新子」將有它自己的mLocationClient,與您的過程中任何其他Service的任何mLocationClient無關。

你似乎是試圖實現的東西我在the preceding comment寫道:

@ jsc123:「你會如何建議我給我LocationClient兩分鐘的窗口前輪詢連接位置」 - 你可以使用AlarmManager,在兩分鐘內調用set()來獲得控制權。

我的意思的事情就是這樣,你會在AlarmManager使用set()getService()PendingIntent傳遞控制權交還給您已經運行Service其已經收購WakeLock,以便它能夠獲得位置,釋放WakeLockstopSelf()

+0

@CommmonsWare,我仍然不確定如何實現您的建議。即使我改變的PendingIntent到的getService(看起來像這樣:意向I =新意圖(theContext,LocalWordService.class); \t \t的PendingIntent thePending = PendingIntent.getService(theContext,0,I, \t \t \t \t PendingIntent.FLAG_CANCEL_CURRENT);)我不明白服務如何接收這個調用,而且我怎樣才能避免一個服務總是在運行的遞歸 - 死亡陷阱。 – NumenorForLife

+0

爲了更清楚,我不確定如何在兩分鐘時間內識別服務,因爲沒有onReceive()方法。對於該方法或類似的方法,我需要使用該位置,斷開客戶端,釋放喚醒鎖,然後終止服務。 – NumenorForLife

+0

@ jsc123:「我不明白服務如何接收該調用」 - 它將通過'onStartCommand()'調用,您可以通過閱讀'PendingIntent'上的getService()文檔來得知: http://goo.gl/f3hW3 – CommonsWare