无法从BroadcastReceiver获取appApplicationContext()

时间:2013-11-14 05:20:53

标签: java android notifications broadcastreceiver

我的应用程序要求我为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);
    }
    }

3 个答案:

答案 0 :(得分:0)

您需要使用context.getResources()context.getApplicationContext(),因此请将context传递给createNotification方法。

答案 1 :(得分:0)

@Pankaj Kumar解决方案的另一种替代方法是将概念作为数据字段存储在onReceive方法中:

public class MyReceiver extends BroadcastReceiver {
    private Context context;

    @Override
    public void onReceive(Context context, Intent arg1) {
        this.context = context;
        createNotification(Calendar.getInstance().getTimeInMillis());
    }

    public void createNotification(long when) {
       // and use 
       // context.getApplicationContext()
    }
 }

答案 2 :(得分:0)

试试这个

public class NotificationAlarm extends BroadcastReceiver {
    public static final String NOTIFICATION_DATA = "NOTIFICATION_DATA";
    String notificationData = "";
    String notificationContent = "Content";
    String notificationTitle = "Title";

    @Override
    public void onReceive(Context context, Intent arg1) {
        createNotification(context,Calendar.getInstance().getTimeInMillis());
    }

    public void createNotification(Context mContext,long when) {
        Bitmap largeIcon = BitmapFactory.decodeResource(mContext.getResources(),
                R.drawable.stamptwo);
        int smalIcon = R.drawable.stamptwoxhdpi;

        Intent intent = new Intent(mContext, Lockscreen.class);
        intent.putExtra(NOTIFICATION_DATA, notificationData);
        intent.setData(Uri.parse("content://" + when));
        PendingIntent pendingIntent = PendingIntent.getActivity(
                mContext, 0, intent,
                Intent.FLAG_ACTIVITY_NEW_TASK);

        NotificationManager notificationManager = (NotificationManager) mContext
                .getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
                mContext)
                .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);
    }
}