从活动的java类npt调用Android通知

时间:2015-04-03 10:20:49

标签: java android

如何从java类开始通知? 我从一项活动开始通知 但是当我尝试从java类中执行它时它不起作用,因为它需要一个上下文(这指的是我的类上下文) 以下是我的函数代码,它从一个活动开始通知:

 public  void sendNotification(){
    NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_camera)
                        .setContentTitle("Someone on your door")
                        .setContentText("Open the camera to see!");

    // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, CameraActivity.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(this);
// Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(CameraActivity.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)      this.getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
        mNotificationManager.notify(CAMERA_NOTIFICATION, mBuilder.build());

    }

提前感谢您的帮助

2 个答案:

答案 0 :(得分:1)

创建一个具有Context作为java类参数的参数化构造函数,并从您的activity中调用它。如何从活动中调用java类。您可能通过使其对象n调用默认构造函数来调用您的java类,我想是这样的:

错误的方式

JavaClass obj = new JavaClass();

正确的方式

`JavaClass obj = new JavaClass(MainActivity.this); // MainActivity.this is` context

答案 1 :(得分:0)

您需要中的上下文。在下面的代码中,我将Context发送给Classes构造函数:

public class SendNotification
    {
        Context context;

        public SendNotification(Context context)
        {
            this.context = context;
        }

         public  void sendNotification()
        {
            NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(context)
                                .setSmallIcon(R.mipmap.ic_camera)
                                .setContentTitle("Someone on your door")
                                .setContentText("Open the camera to see!");

            // Creates an explicit intent for an Activity in your app
                Intent resultIntent = new Intent(context, CameraActivity.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(CameraActivity.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(CAMERA_NOTIFICATION, mBuilder.build());

        }
    }

现在,您可以从活动中创建一个Object,并调用sendNotification方法,如下所示:

SendNotification sendNotification = new SendNotification(this); 
sendNotification.sendNotification();