在运行时更改自定义选项卡操作栏按钮中的额外文本数据
Change extra text data in custom tabs action bar button at runtime
我正在使用 chrome 自定义标签来显示各种 websites.i 在自定义标签操作栏中添加了共享 link 按钮。
builder.setActionButton(bitmap,shareLabel,createPendingShareIntent());
和我的 pendingintent 函数
private PendingIntent createPendingShareIntent() {
Intent actionIntent = new Intent(Intent.ACTION_SEND);
actionIntent.setType("text/plain");
actionIntent.putExtra(Intent.EXTRA_TEXT,getResources().getString(R.string.chromeextra));
return PendingIntent.getActivity(
getApplicationContext(), 0, actionIntent, 0);
}
现在我想更改 Intent.EXTRA_TEXT 以共享 link,具体取决于用户打开的 link。
我知道 PendingIntent.FLAG_UPDATE_CURRENT 但我不知道如何在这种情况下使用。
找了很久才找到解决办法。
我已经通过使用广播接收器完成了这项工作。
首先创建一个自定义广播接收器class来分享link
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);
}
}
}
然后在自定义选项卡构建器中设置菜单项 class
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 自定义标签来显示各种 websites.i 在自定义标签操作栏中添加了共享 link 按钮。
builder.setActionButton(bitmap,shareLabel,createPendingShareIntent());
和我的 pendingintent 函数
private PendingIntent createPendingShareIntent() {
Intent actionIntent = new Intent(Intent.ACTION_SEND);
actionIntent.setType("text/plain");
actionIntent.putExtra(Intent.EXTRA_TEXT,getResources().getString(R.string.chromeextra));
return PendingIntent.getActivity(
getApplicationContext(), 0, actionIntent, 0);
}
现在我想更改 Intent.EXTRA_TEXT 以共享 link,具体取决于用户打开的 link。
我知道 PendingIntent.FLAG_UPDATE_CURRENT 但我不知道如何在这种情况下使用。
找了很久才找到解决办法。 我已经通过使用广播接收器完成了这项工作。
首先创建一个自定义广播接收器class来分享link
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);
}
}
}
然后在自定义选项卡构建器中设置菜单项 class
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);