我引用了示例应用程序代码(https://github.com/googlesamples/android-DirectShare),并将ChooserTargetService添加到我的应用程序
public class ShareChooserTargetService extends ChooserTargetService {
@Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
IntentFilter matchedFilter) {
Log.d("ShareChooserTargetService", "onGetChooserTargets: ");
ComponentName componentName = new ComponentName(getPackageName(),
ShareActivity.class.getCanonicalName());
ArrayList<ChooserTarget> targets = new ArrayList<>();
for (User user : Users.getAll()) {
Bundle extras = new Bundle();
extras.putInt("user_id", user.id);
targets.add(new ChooserTarget(
user.name,
Icon.createWithResource(this, R.mipmap.ic_user),
componentName,
extras));
}
return targets;
}
在Manifest文件中,我添加了这些行:
<activity
android:name=".ui.ShareActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<meta-data
android:name="android.service.chooser.chooser_target_service"
android:value=".ShareChooserTargetService" />
</activity>
<service
android:name=".ShareChooserTargetService"
android:label="@string/app_name"
android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
<intent-filter>
<action android:name="android.service.chooser.ChooserTargetService" />
</intent-filter>
</service>
但是没有调用ShareChooserTargetService,也没有打印日志。我在手机中运行示例应用程序,它运行良好,但我的应用程序中的这些代码不起作用。我无法找到问题所在。有人可以帮忙吗?