Intent
在使用操作创建时,我似乎得到了不一致的结果代码Intent.ACTION_SEND
。
我创建了一个新项目(“基本活动”)并添加了来自 Google 的“将简单数据发送到其他应用程序”示例:
public class MainActivity extends AppCompatActivity {
public static final int SHARE_REQUEST_CODE = 5684;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
share();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void share() {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivityForResult(shareIntent, SHARE_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
}
My problem is that when copy
(some devices have it as copy to clipboard
) is selected, some devices (Pixel 4a, Android 11) get resultCode
of Activity.RESULT_CANCELED
(0) and not Activity.RESULT_OK
(1) like in other devices (Nokia 3.1, Android 9).
我错过了什么还是一个错误?