簡體   English   中英

打開通知按鈕的onclick片段

[英]Opening a Fragment onclick of Notification Button

我最近一直在我的Android應用程序中處理通知。

由於我的應用程序只有1個活動和一些片段。

因此,要處理我的通知按鈕之一,以便它可以打開一個Fragment並調用一個方法(或者可以調用一個包含我所有代碼的方法,包括打開TheFragment)

在此過程中,我可以成功創建通知並使用有效的按鈕,但有一個限制,即只能處理按鈕才能打開任何活動

這是我的通知代碼,請看一看,並建議我該怎么做..

 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()(請詢問是否需要進一步的引用。)請幫我使這成為可能。

您需要從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);

您可以使用相同的“通知”鍵,也可以在其中添加另一個標志,例如-

  intent.putExtra("key_stop",true);

MapsActivity's onCreate()您可以通過檢查標志來實例化片段-

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

    if(stopFlag){
      //Launch your fragment
    }

希望這可以幫助 !!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM