2015-04-03 65 views
1

如何從java類啓動通知? 我開始從活動 通知,但是當我嘗試從一個java類做它,它不工作,因爲它需要一個上下文(這是指我的類上下文) 這是我的函數的代碼開始通知從活動:從活動的java類npt調用Android通知

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()); 

    } 

在此先感謝您的幫助

+0

你需要將'Context'傳遞給正在創建'Notification'的類。您可以在實例化該類時執行此操作。 – 2015-04-03 10:28:21

回答

1

做一個paramaterized其構造背景下的參數爲你的java類,並從你的活動中調用它。你如何從活動中調用你的java類。你可能會使其物N調用默認的構造函數來調用Java類我想是這樣的:

錯誤的方式

JavaClass obj = new JavaClass(); 

正確的方式

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

它的工作原理!謝謝! – sabrina2020 2015-04-03 11:11:44

0

您需要語境類別。在下面的代碼我送背景下的類的構造函數:

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()); 

     } 
    } 

現在,您可以從您的活動創建一個對象,並調用sendNotification的方法是這樣的:

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