[英]Call Android notification from a java class npt from an activity
如何從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());
}
在此先感謝您的幫助
為Java類創建一個以Context作為參數的參數化構造函數,並從您的活動中調用它。 您如何從活動中調用Java類。 您可能通過使其對象n調用默認構造函數來調用Java類,我猜是這樣的:
錯誤道
JavaClass obj = new JavaClass();
正確的路
`JavaClass obj = new JavaClass(MainActivity.this); // MainActivity.this is` context
您需要在類中使用上下文 。 在下面的代碼中,我將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());
}
}
現在,您可以從您的活動中創建一個對象,並像下面這樣調用sendNotification方法:
SendNotification sendNotification = new SendNotification(this);
sendNotification.sendNotification();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.