为广播接收器提供上下文
Provide context to broadcast receiver
我正在开发一个简单的闹钟应用程序,即使该应用程序未启动,它也会通知用户。我有点不知所措,如果我看起来像一个 Java 菜鸟,我很抱歉。
这是遵循本教程:http://www.compiletimeerror.com/2013/10/alarm-example-in-android.html?m=1
它没有提供太多解释,但它大大简化了事情。
我的问题是向广播接收器提供上下文 class。我知道还有其他问题询问如何执行此操作,并且我已经通读了它们。我的问题是,由于我如何在 activity class.
中使用广播接收器,我不知道如何在构造函数中清楚地传递上下文
这就是我的意思。这是单击按钮时将 运行 的代码。我还没有完成它,所以它只是一个骨架 atm。
public void startAlarm(View view) {
Intent intent = new Intent(this, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
}
我需要 AlarmReciever class 中的应用程序上下文,因为我打算发送通知。
按照本教程的设置方式,AlarmReceiver 的用法如下:
AlarmReciever.class
我不确定如何传递上下文,因为我不使用构造函数。我是不是遗漏了一些明显的东西,还是有其他方法可以做到这一点?
你可以试试这个方法:
通过扩展Application简单地创建一个应用程序实例class:
public class App extends Application{
private static Context mContext;
@Override public void onCreate(){
super.onCreate();
App.mContext = getApplicationContext();
}
public static Context getAppContext(){
return App.mContext;
}
}
现在,在需要上下文的 class 中,只需执行以下操作:
// somewhere in your code
App.getAppContext();
注意:您可能必须在 AndroidManifest xml 文件中设置 Application 属性并指向您的应用 class!
祝你好运,编码愉快!
您应该将上下文传递给您的接收器:
public class AlarmReceiver extends BroadcastReceiver {
private Context context;
private PowerManager pm;
private PowerManager.WakeLock mWakeLock;
@Override
public void onReceive(Context context, Intent intent)
{
this.context = context;
pm = (PowerManager)context.getSystemService(context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,TAG);
wl.acquire();
//....do your work.
wl.release();
}
}
我正在开发一个简单的闹钟应用程序,即使该应用程序未启动,它也会通知用户。我有点不知所措,如果我看起来像一个 Java 菜鸟,我很抱歉。
这是遵循本教程:http://www.compiletimeerror.com/2013/10/alarm-example-in-android.html?m=1 它没有提供太多解释,但它大大简化了事情。
我的问题是向广播接收器提供上下文 class。我知道还有其他问题询问如何执行此操作,并且我已经通读了它们。我的问题是,由于我如何在 activity class.
中使用广播接收器,我不知道如何在构造函数中清楚地传递上下文这就是我的意思。这是单击按钮时将 运行 的代码。我还没有完成它,所以它只是一个骨架 atm。
public void startAlarm(View view) {
Intent intent = new Intent(this, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
}
我需要 AlarmReciever class 中的应用程序上下文,因为我打算发送通知。 按照本教程的设置方式,AlarmReceiver 的用法如下:
AlarmReciever.class
我不确定如何传递上下文,因为我不使用构造函数。我是不是遗漏了一些明显的东西,还是有其他方法可以做到这一点?
你可以试试这个方法:
通过扩展Application简单地创建一个应用程序实例class:
public class App extends Application{
private static Context mContext;
@Override public void onCreate(){
super.onCreate();
App.mContext = getApplicationContext();
}
public static Context getAppContext(){
return App.mContext;
}
}
现在,在需要上下文的 class 中,只需执行以下操作:
// somewhere in your code
App.getAppContext();
注意:您可能必须在 AndroidManifest xml 文件中设置 Application 属性并指向您的应用 class!
祝你好运,编码愉快!
您应该将上下文传递给您的接收器:
public class AlarmReceiver extends BroadcastReceiver {
private Context context;
private PowerManager pm;
private PowerManager.WakeLock mWakeLock;
@Override
public void onReceive(Context context, Intent intent)
{
this.context = context;
pm = (PowerManager)context.getSystemService(context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,TAG);
wl.acquire();
//....do your work.
wl.release();
}
}