我的应用程序要求我为 getResources() 或 getApplicationContext() 创建一个方法。
不允许在 BroadcastReceiver 中调用这些方法吗?
@Override
public void onReceive(Context context, Intent arg1) {
createNotification(Calendar.getInstance().getTimeInMillis());
}
public void createNotification(long when) {
Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.stamptwo);
int smalIcon = R.drawable.stamptwoxhdpi;
Intent intent = new Intent(context.getApplicationContext(),
Lockscreen.class);
intent.putExtra(NOTIFICATION_DATA, notificationData);
intent.setData(Uri.parse("content://" + when));
PendingIntent pendingIntent = PendingIntent.getActivity(
context.getApplicationContext(), 0, intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationManager notificationManager = (NotificationManager) context
.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
context.getApplicationContext())
.setWhen(when)
.setContentText(notificationContent)
.setContentTitle(notificationTitle)
.setSmallIcon(smalIcon)
.setAutoCancel(true)
.setTicker(notificationTitle)
.setLargeIcon(largeIcon)
.setDefaults(
Notification.DEFAULT_LIGHTS
| Notification.DEFAULT_VIBRATE
| Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
Notification notification = notificationBuilder.build();
notificationManager.notify((int) when, notification);
}
}