我有一个每日警报,其中应包含我只能从活动上下文中获取的文本 - 该活动会根据用户的设置生成一个密钥,以获取特定日期的特定生成信息。
即使应用程序未运行,通知也会在每天上午10点从BootReceiver和AlarmManager启动。
如何将BroadcastReceiver上下文转换为活动,或者为了使用其方法而无形地运行活动?
这就是我设置闹钟的方法:
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, AlarmManager.INTERVAL_DAY, pendingIntent);
这是BroadcastReceiver:
public class PushNotificationReceiver extends BroadcastReceiver {
private Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
String notificationTitle = intent.getExtras().getString("notificationTitle");
String notificationText;
boolean dailyNotification = intent.getExtras().getBoolean("dailyNotification ", false);
int requestCode = intent.getExtras().getInt("requestCode");
if(dailyNotification ) {
Log.d("DAILY","DAILY");
// HERE IS WHERE I NEED A BUNCH OF METHODS FROM THE ACTIVITY TO GENERATE THE TITLE AND THE TEXT FOR THE NOTIFICATION
notificationTitle = "ASD";
notificationText = "asd";
} else {
notificationText = intent.getExtras().getString("notificationText");
}
String soundResource = "android.resource://com.asdqwe.asd/raw/tone";
Uri soundUri = Uri.parse(soundResource);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setContentTitle(notificationTitle)//
.setContentText(notificationText)//
.setSmallIcon(R.drawable.app_icon)//
.setSound(soundUri)//
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.app_icon));//
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, MyActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MyActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(543, mBuilder.build());
} // End of onReceive