在Android中使用传递额外的意图值报警管理 [英] Passing values using intent extras in Alarm Manager in android

查看:160
本文介绍了在Android中使用传递额外的意图值报警管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果这code将工作(我不能尝试,现在。而且,我有必须清除一些疑虑)。

I want to know if this code will work(I cannot try it out right now. Moreover, I have a few doubts that have to be cleared).

Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("user",global.getUsername());
intent.puExtra("password",global.getPassword);
PendingIntent sender = PendingIntent.getBroadcast(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
Log.v("inside log_run", "new service started");
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, IMMEDIATELY,60000,sender);
finish();

如你所见,这code开头的 AlarmManager setRepeating()。如果你看到的意图(实际上是待定意向)传递到广播接收器,但是也有一些通过在两个临时演员。这些是只要应用程序正在运行该活全局变量。但这 AlarmManager 意味着在后台运行(即应用将是活着只为前几个调用的alrmamanager到广播recevier的)
结果
我的提问
结果
将AlarmManager使全局变量(用户名和密码)的副本,并维持与意图一起传递这个副本(临时演员)?还是会引用的全局变量(在这个过程中传递null作为额外当全局变量成为空)?因为,这些值将在广播接收器中使用。

As you can see, this code starts an AlarmManager with setRepeating(). If you see the intent(actually the pending intent) passed on to the BroadcastReceiver, there are two extras that are passed on. These are global variables that live as long as the Application is running. But this AlarmManager is meant to be run in the background (that is application will be alive only for the first few calls of the of the alrmamanager to the broadcast recevier)
My Question
Will AlarmManager make a copy of the global variables(the username and password) and maintain this copy to be passed along with the intent (as extras)? Or will it reference the global variables (in the process pass null as the extra when the global variables become null)? Because, these values will be used in the broadcast receiver.

推荐答案

我可以看到这个方法出现的问题。我会做的是你的两个变量存储在共享preFS文件,并直接从BroadcastReciever访问它们。共享preFS值持续,而在Android的全局变量没有。

I can see problems arising from this method. What I would do is store your two variables in a SharedPrefs file and access them from the BroadcastReciever directly. SharedPrefs values persist while "global variables" in Android do not.

这篇关于在Android中使用传递额外的意图值报警管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆