我最近一直在处理我的Android应用程序中的通知。
由于我的应用程序只有一个片段的活动..
所以,要处理我在通知上的按钮之一就可以了 打开片段并调用方法(或者它可以调用包含我所有代码的方法,包括打开片段)
在此过程中,我成功建立了一个通知和工作按钮但是有一个限制,只能处理该按钮以打开任何活动
这是我的通知代码,请查看一下 请建议我该怎么做..
public int getNotification(View view, String Data) {
// NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
Context context = view.getContext();
Intent intent = new Intent(context.getApplicationContext(), MapsActivity.class);
intent.putExtra("Notification","1");
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, 0);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//Intent for "Navigate" Button on Notification
String lat = String.valueOf(dest.latitude);
String lon = String.valueOf(dest.longitude);
String format = "geo:0,0?q=" + lat + "," + lon + "Destination Point";
Uri uri = Uri.parse(format);
Intent intentNavigate = new Intent(Intent.ACTION_VIEW, uri);
PendingIntent pIntentNavigate = PendingIntent.getActivity(context.getApplicationContext(), 0, intentNavigate, 0);
if (Build.VERSION.SDK_INT < 16) {
getToast("API below 16 ...notification not supported");
} else {
//Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_car);
Notification.Builder builder = new Notification.Builder(context);
//builder.setLargeIcon(icon);
builder.setSmallIcon(R.drawable.icar);
builder.setContentTitle("Car Rental Application");
builder.setContentText(Data);
builder.setOngoing(true);
builder.setSound(soundUri);
builder.setContentIntent(pendingIntent);
builder.addAction(R.drawable.ic_gps, "NAVIGATE ", pIntentNavigate);
builder.addAction(R.drawable.ic_stop, "STOP RIDE", pendingIntent);
Notification notify = builder.build();
notificationManager.notify(notificationID, notify);
}
return notificationID;
}
public void removeNotification() {
NotificationManager manager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(
notificationID);
getToast("Notification Removed");
}
我必须处理通知中的第二个动作(STOP RIDE),它应该调用一个函数stopride() (请询问是否需要进一步参考) 请帮我把它变成可能的.. 提前致谢
答案 0 :(得分:0)
您需要从getIntent()
MapsActivity
中获取您的值
现在,您尝试使用此意图调用{STOP 1'上的MapsActivity
-
Intent intent = new Intent(context.getApplicationContext(), MapsActivity.class);
intent.putExtra("Notification","1");
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, 0);
您可以使用相同的“通知”键或在此添加一个标志 -
intent.putExtra("key_stop",true);
在MapsActivity's onCreate()
中,您可以通过检查标记来实例化您的片段 -
boolean stopFlag = getIntent().getBooleanExtra("key_stop",false);
if(stopFlag){
//Launch your fragment
}
希望这会有所帮助!!