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

toメソッドを使用するcontext.getResources()context.getApplicationContext()、渡す必要があります。contextcreateNotification

于 2013-11-14T05:22:40.030 に答える
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 に答える
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 に答える