[英]Android AlarmManager and BroadcastReceiver clarfications
I am trying to write an android application that will silence the phone at a certain time, and to do that I am using a combination of an AlarmManager
and BroadcastReceiver
as per this tutorial . 我正在尝试编写一个可在特定时间使手机静音的android应用程序,并且为此我按照本教程使用
AlarmManager
和BroadcastReceiver
的组合。 I am using a separate class that will contain the method to set up an event using the AlarmManager
so that I can use the method in more than one of my activities. 我正在使用一个单独的类,该类将包含使用
AlarmManager
设置事件的方法,以便可以在多个活动中使用该方法。 The code for this class is as follows: 该类的代码如下:
public class EventScheduler {
public static void schedule(Event event) {
Calendar start = event.getStartTime();
Calendar end = event.getEndTime();
String status = event.getStatus();
Context ctx;
Intent intent = new Intent(ctx, AlarmReceiver.class);
intent.putExtra("start_time", start);
intent.putExtra("end_time", end);
intent.putExtra("status", status);
PendingIntent sender = PendingIntent.getBroadcast();
AlarmManager am = (AlarmManager) getSystemSerivce(ALARM_SERVICE);
}
public static void unschedule(Event event) {
}
}
My first question is about initializing the intent object. 我的第一个问题是关于初始化意图对象。 Since this method is defined in a class that does not extend Activity it does not have a context (
ctx
), but this method will be used in classes that do extend Activity, so how would I get the context of those classes to use in the initialization of the intent? 由于此方法是在不扩展Activity的类中定义的,因此它没有上下文(
ctx
),但是将在确实扩展Activity的类中使用此方法,因此,我将如何获取这些类的上下文以用于扩展初始化意图?
My second question is about initializing the PendingIntent. 我的第二个问题是关于初始化PendingIntent。
PendingIntent.getBroadcast()
is supposed to take 4 parameters, so again how would I get the context of the class calling this method to use as the first parameter? PendingIntent.getBroadcast()
应该采用4个参数,因此我又如何获取调用此方法的类的上下文用作第一个参数? Also in the documentation it says that the second parameter, the requestCode is not used, does that mean this can be 0
? 同样在文档中,它表示未使用第二个参数requestCode,这是否意味着它可以为
0
?
My third question is about initializing the AlarmManager. 我的第三个问题是关于初始化AlarmManager。 Again the
ALARM_SERVICE
field is from a context object, so what context object would I use in this case? 同样,
ALARM_SERVICE
字段来自上下文对象,因此在这种情况下我将使用哪个上下文对象?
Answering your question on context, you should pass the ApplicationContext
and not directly the Activity
in case you need the context throughout the applications lifecycle. 在回答有关上下文的问题时,如果在整个应用程序生命周期中需要上下文,则应该传递
ApplicationContext
而不是直接传递Activity
。 If you pass your Activity
, the GarbageCollector
can't remove it from memory when it's no longer needed, which can cause memory leaks. 如果您通过
Activity
,则GarbageCollector
无法在不再需要它时将其从内存中删除,这可能导致内存泄漏。 You can get the application context using context.getApplicationContext()
. 您可以使用
context.getApplicationContext()
获得应用程序上下文。
Hope that helps. 希望能有所帮助。
You should pass your Context
into each method (in your case, most likely your Activity
): 您应该将
Context
传递给每个方法(在您的情况下,很可能是Activity
):
public static void schedule(Context ctx, Event event) {
// ...
}
public static void unschedule(Context ctx, Event event) {
// ...
}
And yes, requestCode
for PendingIntent.getBroadcast()
can always be zero. 是的,
PendingIntent.getBroadcast()
requestCode
始终可以为零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.