【问题标题】:Launch a fragment in my Android application from the notification bar从通知栏在我的 Android 应用程序中启动一个片段
【发布时间】:2013-08-22 11:16:59
【问题描述】:

如何从通知栏中的通知启动我的 Android 应用程序中的片段?

我尝试实现 this answer 创建自己的操作,然后将操作设置为意图,但我不确定如何使用它以及还需要什么 - 比如向清单添加一些东西。

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

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

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

}

【问题讨论】:

    标签: action android-notifications android-pendingintent intentfilter


    【解决方案1】:

    所以这实际上很容易。希望我也可以帮助其他人看到这一点。

    我向这个notify 函数发送一个动作。我将该操作添加到我的intent 以启动一项活动。在我的例子中,我打开了启动活动,因为所有片段都是根据用户的操作从该活动中加载的。所以我使用setAction 设置动作,并在下面的活动中使用该动作。

    我的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);
    }
    

    然后在我加载片段的activity 中,我得到动作并过滤它:

    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 函数中,如果所需的片段仍在内存中(意味着用户较早在那里并且尚未销毁),我将替换该片段,或者我创建一个所需的新片段。

    【讨论】:

    • 如何检查片段是否在内存中??
    • String[] frag_tags = { "fragment1", "fragment2", "fragment3"};片段片段 = getSupportFragmentManager().findFragmentByTag(frag_tags[fragNumber]); if (frag != null) { Log.d(TAG, "Frag 在内存中:" + String.valueOf(fragNumber)); transaction.replace(R.id.lin_main_container, frag, frag_tags[fragNumber]); } else { Log.d(TAG, "重新创建碎片:" + String.valueOf(fragNumber)); }
    • 为什么我的操作无效?
    • 第二部分代码是用OnCreate还是NewIntent写的?当 Activity 已经运行或未运行时如何处理。
    • 考虑为你的片段使用标签'这更容易
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多