X-H*_*Man 4 android android-pendingintent chrome-custom-tabs share-intent
我想使用CustomTabs库,我需要添加一个共享菜单项.该库只接受PendingIntent实例作为菜单项的Action.我想使用以下代码确保在没有Just Once和Always按钮的情况下始终向用户建议列表:
Intent shareIntent = Intent.createChooser(intent, "Choose the application to share.");
Run Code Online (Sandbox Code Playgroud)
但问题是,如果我使用此选择器Intent创建PendingIntent,则CustomTabs for Chrome不会为用户激活选择器:
PendingIntent pendingIntent = PendingIntent.getActivity(context,
requestCode,
shareIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
有没有办法使用Chooser的Intent作为PendingIntent?
我不能使用以下行来启动Intent,因为库是这样做的,它只接受PendingIntents:
startActivity(Intent.createChooser(i, getString()));
Run Code Online (Sandbox Code Playgroud)
您可以使用广播接收器来实现此目的.
首先创建一个自定义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);
}
}
Run Code Online (Sandbox Code Playgroud)
}
然后在自定义选项卡构建器类中设置菜单项
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
837 次 |
| 最近记录: |