ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 如何使用选择器中的Intent作为PendingIntent

2019-08-28 18:24:22  阅读:202  来源: 互联网

标签:chrome-custom-tabs android android-pendingintent share-intent


我想使用CustomTabs库,我需要添加一个共享菜单项.该库只接受PendingIntent实例作为菜单项的Action.我想使用以下代码确保在没有Just Once和Always按钮的情况下始终向用户建议列表:

Intent shareIntent = Intent.createChooser(intent, "Choose the application to share.");

但问题是,如果我使用此选择器Intent创建PendingIntent,则CustomTabs for Chrome不会为用户激活选择器:

PendingIntent pendingIntent = PendingIntent.getActivity(context,
            requestCode,
            shareIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

有没有办法使用Chooser的Intent作为PendingIntent?

我无法使用以下行来启动Intent,因为该库正在执行此操作并且它只接受PendingIntents:

startActivity(Intent.createChooser(i, getString()));

解决方法:

您可以使用广播接收器来实现此目的.

首先创建一个自定义broadcastreciever类来创建要分享的选择器

ShareBroadcastReceiver.java

public class ShareBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String url = intent.getDataString();
    if (url != null) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT,context.getResources().getString(R.string.chromeextra)+ url);

        Intent chooserIntent = Intent.createChooser(shareIntent, "Share url");
        chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        context.startActivity(chooserIntent);
    }
}

}

然后在自定义选项卡构建器类中设置菜单项

  String shareLabel = getString(R.string.label_action_share);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
        android.R.drawable.ic_menu_share);

//Create a PendingIntent to your BroadCastReceiver implementation
Intent actionIntent = new Intent(
        this.getApplicationContext(), ShareBroadcastReceiver.class);
PendingIntent pendingIntent = 
        PendingIntent.getBroadcast(getApplicationContext(), 0, actionIntent, 0);            

//Set the pendingIntent as the action to be performed when the button is clicked.            
intentBuilder.setActionButton(icon, shareLabel, pendingIntent);

标签:chrome-custom-tabs,android,android-pendingintent,share-intent
来源: https://codeday.me/bug/20190828/1754437.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有