Android 在服务中注册广播接收机

Android 在服务中注册广播接收机,android,Android,我的应用程序有问题,它从服务器获取数据,并决定何时提醒,我在意识到此alhoritm正在使用时犯了错误,因为在睡眠模式下服务正在停止,我需要将其替换为警报,但我的服务中有很多变量,将它们发送到AlarmReceiver类是个问题,我读过关于内部类的文章,但我无法在清单中注册它。它只能是静态的,我尝试使用register receiver,但它不工作。我对它做了什么错误 public class AlarmReceiver extends BroadcastReceiver { priv

我的应用程序有问题,它从服务器获取数据,并决定何时提醒,我在意识到此alhoritm正在使用时犯了错误,因为在睡眠模式下服务正在停止,我需要将其替换为警报,但我的服务中有很多变量,将它们发送到AlarmReceiver类是个问题,我读过关于内部类的文章,但我无法在清单中注册它。它只能是静态的,我尝试使用register receiver,但它不工作。我对它做了什么错误

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>

它只能是

public static class AlarmReceiver
我相信它应该被引用为

android:name="com.autobus66.autobus66.service.Autobus66Service.AlarmReceiver"
静态,因为内部类的实例具有对包含类的包含实例的隐式引用。不能在包含类的实例方法之外创建内部类的非静态实例,因为java需要指向该包含类实例的指针

这意味着您必须找到某种方法来保持应用程序的状态。还要注意,任何进程都可能被终止和重新启动(包括服务和活动),并且应该编写应用程序,以便终止进程不会导致数据丢失。(我不说这是好的,我说这是好的。)