简体   繁体   English

打开通知按钮的onclick片段

[英]Opening a Fragment onclick of Notification Button

I have been recently working on Notifications in My android app. 我最近一直在我的Android应用程序中处理通知。

As my Application have only 1 activity with some fragments.. 由于我的应用程序只有1个活动和一些片段。

So, to handle my one of the Button On the Notification so that it can Open a Fragment and call a method (or it can call a method which contains all my code including opening The fragment ) 因此,要处理我的通知按钮之一,以便它可以打开一个Fragment并调用一个方法(或者可以调用一个包含我所有代码的方法,包括打开TheFragment)

In course of this I am sucessfull in building a notification and with working button But with a limitation that the button can only be handled to open any activity 在此过程中,我可以成功创建通知并使用有效的按钮,但有一个限制,即只能处理按钮才能打开任何活动

Here's my code of Notification , Please take a look in it and Please suggest how should I do it .. 这是我的通知代码,请看一看,并建议我该怎么做..

 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");
}

I have to Handle my 2nd Action in the Notification (STOP RIDE) which should call a function stopride() (Please ask if further ref. needed) Please help me making this possible for me .. Thanks in advance 我必须在通知(STOP RIDE)中处理我的第二个操作,该操作应调用函数stopride()(请询问是否需要进一步的引用。)请帮我使这成为可能。

You need to get your values from getIntent() in MapsActivity Now you are trying to invoke MapsActivity on "STOP RIDE" action with this intent - 您需要从MapsActivity getIntent()获取值现在,您正试图以此MapsActivity在“ STOP RIDE”操作上调用MapsActivity

     Intent intent = new Intent(context.getApplicationContext(), MapsActivity.class);
        intent.putExtra("Notification","1");
  PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, 0);

You can use the same key "Notification" or add one more flag in this like - 您可以使用相同的“通知”键,也可以在其中添加另一个标志,例如-

  intent.putExtra("key_stop",true);

In MapsActivity's onCreate() you can instantiate your fragment by checking your flag - MapsActivity's onCreate()您可以通过检查标志来实例化片段-

    boolean stopFlag = getIntent().getBooleanExtra("key_stop",false);

    if(stopFlag){
      //Launch your fragment
    }

Hope this helps !! 希望这可以帮助 !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM