从通知栏在我的Android应用程序中启动片段 [英] Launch a fragment in my Android application from the notification bar

查看:60
本文介绍了从通知栏在我的Android应用程序中启动片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从通知栏中的通知开始在Android应用程序中创建片段?

How do I start a fragment in my Android application from a notification in the notification bar?

我尝试实现创建我自己的动作,然后将动作设置为意图的答案,但是我不确定如何使用它以及需要什么,例如添加一些东西

I've tried to implement this answer of creating my own action and then setting the action to the intent, but I'm unsure how to use it and what is required additionally - like adding something to the Manifest.

我有一个通知类,该类接收上下文,消息和操作。然后,我想根据该操作进行筛选,以确定要启动哪个片段,但是我不知道如何启动片段而不是启动活动。

I've got a notification class that receives a context, a message and then an action. I then want to filter on that action to determine which fragment to launch, but I don't know how to launch a fragment as opposed to launching an activity.

这里是我的Notifications.java类(不完整):

Here is my Notifications.java class (incomplete):

public class Notifications {

    private Context mContext;

    public Notifications(Context context) {
        this.mContext = context;
    }

    public static void notify(Context context, String message, String  action) {

        //Action you invent should include the application package as a prefix — for example: "com.example.project.SHOW_COLOR".
        action = "my.package.name.here.frag."+action;

        //Construct a user message.
        String appName = context.getResources().getString(R.string.app_name);

        // Use the Notification manager to send notification
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        // Create a notification using android stat_notify_chat icon. 
        Notification notification = new Notification(R.drawable.ic_stat_notification, message, 0);

        //Sound, lights, vibration.
        //REMEMBER PERMISSIONS.
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;

        // Create a pending intent to open the application when the notification is clicked.
        //Restart the app.
        Intent launchIntent = null;

        //Get the action and based on what the action is, launch the application displaying the appropriate fragment.
        if (action.equalsIgnoreCase("friend")){
            //New friend notification
            //Launch application displaying the list of friends


        }
        if (action.equalsIgnoreCase("article")){
            //New article has been posted
            //Launch application displaying the news feed fragment


        }
        if (action.equalsIgnoreCase("points")){
            //Points scored notification
            //Launch application displaying the user's profile


        }
        if (action.equalsIgnoreCase("redeemable")){
            //New redeemable is offered
            //Launch application displaying the list of redeemables


        }
        if (!action.equalsIgnoreCase("friend") 
                && !action.equalsIgnoreCase("article") 
                && !action.equalsIgnoreCase("points") 
                && !action.equalsIgnoreCase("redeemable")){
            //Not specific, so launch the application from scratch displaying the activity feed

            launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        }



        if(action != null && launchIntent != null){         
            launchIntent.setAction(action);         
        }

        // Set the notification and register the pending intent to it
        notification.setLatestEventInfo(context, appName, message, pendingIntent);

        // Trigger the notification
        notificationManager.notify(0, notification);
    }

}


推荐答案

所以这实际上很容易。希望我也能帮助其他人。

So this was actually pretty easy. Hopefully I can help someone else see this too.

我向 notify 函数发送一个动作。我将该动作添加到我的意向中以启动活动。就我而言,我打开了启动活动,因为所有片段都是根据用户的操作从该活动中加载的。因此,我使用 setAction 设置了动作,然后在活动中使用该动作,如下所示。

I send an action to this notify function. The I add that action to my intent to launch an activity. In my case I open the launching activity, because all the fragments are loaded from within that activity based on what the user does. So I set the action using setAction and the I use that action in the activity as below.

我的 Notifications.java 类更改为:

public static void notify(Context context, String message, String  action) {

    action = action.toUpperCase();

    // Create a pending intent to open the the application when the notification is clicked.
    //Restart the app.
    Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());

    if(action != null && launchIntent != null){         
        launchIntent.setAction(action);         
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(context, -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.when = System.currentTimeMillis();  
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    // Set the notification and register the pending intent to it
    notification.setLatestEventInfo(context, appName, message, pendingIntent);

    // Trigger the notification
    notificationManager.notify(0, notification);
}

然后在我的活动中从我加载片段的地方,我得到了动作并对其进行了过滤:

And then in my activity from where I load the fragments, I get the action and filter it:

Intent intent = getIntent();

try{
    String action = intent.getAction().toUpperCase();

    if(action != null){
        if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_friend))){
                goFrag(getResources().getInteger(R.integer.FRAG_A_INT));
            }
        if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_article))){
                goFrag(getResources().getInteger(R.integer.FRAG_B_INT));
            }
        if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_points))){
                goFrag(getResources().getInteger(R.integer.FRAG_C_INT));
            }
        if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_redeemable))){
                goFrag(getResources().getInteger(R.integer.FRAG_D_INT));
            }
        if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_dance))){
                goFrag(getResources().getInteger(R.integer.FRAG_E_INT));
            }
        }else{
            Log.d(TAG, "Intent was null");
        }
    }catch(Exception e){
        Log.e(TAG, "Problem consuming action from intent", e);              
    }

在我的 goFrag 中函数如果所需的片段仍在内存中(意味着用户早些时候在这里并且尚未被销毁),我将替换该片段,或者创建一个新的所需片段。

In my goFrag function I replace the fragment if the required fragment is still in memory (meaning the user was there earlier and it hasn't been destroyed yet), or I create a new fragment required.

这篇关于从通知栏在我的Android应用程序中启动片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆