如何從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());
}
在此先感謝您的幫助
你需要將'Context'傳遞給正在創建'Notification'的類。您可以在實例化該類時執行此操作。 – 2015-04-03 10:28:21