我使用的警报管理器调用在稍后的时间,我想更新在用户指定的时间文件的服务。该机制工作正常。我现在需要做的是传递给警报所称的服务,因为我有多个不同意图的警报,需要在不同的时间做不同的事情。传递信息从一个活动到服务
我明白如何通过额外使用包,但它似乎并没有与服务工作。我无法通过这种方式传递任何信息,我一直收到null
作为从活动传递到服务的内容。
这里是1个报警我的活动代码。
Intent myIntent = new Intent(this, TimerService.class);
Bundle bundle = new Bundle();
bundle.putString("extraData", "FIRST_ALARM");
myIntent.putExtras(bundle);
PendingIntent AmPendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, Time2fire, fONCE_PER_DAY, AmPendingIntent);
服务代码:
super.onStart(intent, startId);
String bundleFromActivity = intent.getStringExtra("extraData");
我搜索了很多,但没有我见过任职。从我的服务 从我的活动
Intent intent = new Intent(getApplicationContext(), TimerService.class);
intent.putExtra("someKey", "hifromalarmone");
PendingIntent myIntent = PendingIntent.getService(getApplicationContext(),0,intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, Time2fire, fONCE_PER_DAY, myIntent);
我知道现在的OnStart被弃用,onstartcommand必须使用:
确定,所以现在我改成了这一点。
public int onStartCommand(Intent intent, int startId, int flags)
{
super.onStartCommand(intent, flags, startId);
Bundle extras = intent.getExtras();
String data1 = extras.getString("somekey");// intent.getStringExtra("someKey");
return START_STICKY;
}
并猜测什么....仍返回一个空。我在这里错过了什么?看起来我没有通过正确的东西。
好吧,所以我想通了......经过很多挖掘和一点点好运,我意识到仅仅更新我的意图内的数据是不够的,因为我的原始意图已经在系统中注册。因此,它从未更新过我传递的新数据。这里是关键,(希望有人认为这有用) 下面的代码行就是需要更新 的PendingIntent AmPendingINtent = PendingIntent.getService(此,0,myIntent,0); 如果更新上次0,因为这是在系统中注册的最后一次别的东西它迫使意图更新并沿着你的包通过。
的PendingIntent AmPendingINtent = PendingIntent.getService(此,0,myIntent,654654); //这样的事情。
是这个Java的Android?可能会从一些适当的标签中受益。 – joshp
正确。我为这个谢谢添加了其他标签! – FirmwareEngineer
您可能想在这里查看我的教程:http://blog.blundell-apps.com/notification-for-a-user-chosen-time/我发送了一个布尔额外的意图 – Blundell