Android M直接分享

时间:2015-06-24 16:27:32

标签: android

我有两项活动和服务。我正在尝试直接分享,但它不起作用(

发送数据的第一个活动

public class MainShareActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_share_main);
}

public void share(View view){
    startService(new Intent(this, DirectSharePicker.class));

    EditText etShare = (EditText) findViewById(R.id.sharedText);

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, etShare.getText().toString());

    startActivity(Intent.createChooser(intent, "Direct share demo"));
}

活动,捕获SEND

public class CatchSendActivity extends AppCompatActivity {

public static final String EXTRA_USER_ID = "userId";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shared);

    TextView textView = (TextView) findViewById(R.id.result);

    Intent intent = getIntent();
    int userID = intent.getIntExtra(EXTRA_USER_ID, -1);
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);

    textView.setText(userID + ": " + sharedText);
}

目标服务

public class DirectSharePicker extends ChooserTargetService {

@Override
public List<ChooserTarget> onGetChooserTargets(ComponentName componentName, IntentFilter intentFilter) {
    List<ChooserTarget> response = new LinkedList<>();
    Icon icon = Icon.createWithResource(this, R.id.icon);
    for (int i = 0; i < 5; i++){
        String title = "Target" + i;

        Intent intent = new Intent(this, CatchSendActivity.class);
        intent.putExtra(CatchSendActivity.EXTRA_USER_ID, i);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        ChooserTarget chooserTarget = new ChooserTarget(title, icon, i, pendingIntent);
        response.add(chooserTarget);
    }
    return response;
}

这是我的明白

<activity
        android:name=".MainShareActivity"
        android:label="@string/app_name"/>

    <service
        android:name=".DirectSharePicker"
        android:label="SHARING!"
        android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
        <intent-filter>
            <action android:name="android.service.chooser.ChooserTargetService" />
        </intent-filter>
    </service>

    <activity
        android:name=".CatchSendActivity"
        android:screenOrientation="portrait">

        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/plain" />
        </intent-filter>

        <meta-data
            android:name="android.service.chooser.chooser_target_service"
            android:value=".DirectSharePicker" />

    </activity>

但onGetChooserTargets从未打过电话。

1 个答案:

答案 0 :(得分:0)

来自Android Developer Pages的文档:

  

目标的创建者可以提供排名分数。假设该分数相对于同一查询提供的其他目标。分数应该在0.0f(不太可能匹配)到1.0f(非常相关的匹配)的范围内。一组目标的分数不需要总和为1.

尝试输入一些有效的分数,而不是&#39; ,因为根据您的代码我可以达到4分。

此外,在清单中输入类和服务的全名 e.g

<service
    android:name="com.your.package.DirectSharePicker"
    ...

相反

<service
    android:name=".DirectSharePicker"
    ...