2013-12-17 201 views
0

我有我的應用程序的問題,它需要從服務器的數據,並決定何時提醒,我沒有錯誤時,實現這種alhoritm服務,因爲在睡眠模式服務停止,我需要更換這與報警但我在服務中有很多變量,將它們發送到AlarmReceiver類是有問題的,我讀了關於內部類的內容,但是我無法在清單中註冊,它只能是靜態的,我嘗試使用寄存器接收器,但它是不工作我做錯了什麼?註冊broadcastReceiver服務

public class AlarmReceiver extends BroadcastReceiver { 
    private String text; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); 
     wl.acquire(); 
     Autobus66Service.UpdatePassages(); 
     wl.release(); 
    } 

    public void SetAlarm(Context context,String text){ 
     this.text = text; 
     AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(context, AlarmReceiver.class); 
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 30 * 1, pi); 
    } 

    public void CancelAlarm(Context context) 
    { 
     Intent intent = new Intent(context, AlarmReceiver.class); 
     PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     alarmManager.cancel(sender); 
    } 
} 


@Override 
public void onCreate() { 

    Log.d("service onCreate", MainActivity.TAG); 
    handler = new Handler(); 
    intent = new Intent(AppNames.BROADCAST_ACTION); 
    ... 

    alarm = new AlarmReceiver(); 
    IntentFilter filter= new IntentFilter(); 
    registerReceiver(alarm,filter); 

} 


@Override 
public void onStart(Intent intent, int startId) { 
    Log.d("service onStart", MainActivity.TAG); 

    handler.removeCallbacks(sendUpdatesToUI); 
    handler.postDelayed(sendUpdatesToUI, 1000); 
    createNotification(); 
    alarm.SetAlarm(this,"Some text"); 

} 

和AndroidManifest它看起來像:

<receiver android:process=":remote" android:name="com.autobus66.autobus66.service.Autobus66Service$AlarmReceiver"> 

    </receiver> 

回答

0

它只能

public static class AlarmReceiver 

,我相信它應該被引用作爲

android:name="com.autobus66.autobus66.service.Autobus66Service.AlarmReceiver" 

static,因爲內部類的實例具有對包含類的包含實例的隱式引用。您不能在包含類的實例方法之外創建內部類的非靜態實例,因爲java需要一個指向包含類的實例的指針。

這意味着你必須找到一些方法來保持你的應用程序的狀態。還要注意的是,任何進程可能會被終止並重新啓動(包括服務和活動),並且應該編寫應用程序,以便殺死進程不會導致數據丟失。 (我不說這很好,我說是這樣。)