0

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

3 回答 3

0

您将需要使用context.getResources()context.getApplicationContext()传递contexttocreateNotification方法。

于 2013-11-14T05:22:40.030 回答
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()
    }
 }
于 2013-11-14T05:27:58.357 回答
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);
    }
}
于 2013-11-14T05:29:41.813 回答