[英]having issues with starting an activity using Intent
我對如何使功能正常工作感到沮喪。 我一直在研究和尋找有關Intent的信息。
起初我以為我做對了,但是我錯了,我對自己的意思做了一些概述。 我制作了一個帶有6個按鈕的應用程序,所有這些按鈕都可以打開不同的應用程序。
這是我啟動聯系人應用程序的onClick方法。
// Contacts Launch
Button contacts_launch = (Button) findViewById(R.id.contacts_launch);
contacts_launch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_contacts = new Intent(Intent.ACTION_MAIN);
intent_contacts.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent_contacts);
}
});
我所有按鈕的onClick Intent方法都是相同的,只是意圖名稱已根據應用程序名稱進行了更改,例如消息傳遞是intent_message。
啟動應用程序時,以及我點擊按鈕時。 它通過一個窗口提示我,我可以在其中選擇應用程序。 每當我選擇按鈕時,它就會運行該應用程序。
但是,當我選擇另一個應用程序時,它會啟動通訊錄應用程序嗎? 而且不要讓我像以前那樣選擇它。 我怎樣才能解決這個問題? 我很確定我使用的意圖函數錯誤。
謝謝你的時間。
請再次檢查代碼,那是我修改后的代碼,但是沒有用,只有一種intent方法。 我將最初使用的代碼添加到可以選擇的位置。 那就是意圖和類別。 (您現在可以看到的那個)
嗨,請使用以下代碼打開聯系人:
@SuppressWarnings("deprecation")
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivity(intent);
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", urlToShare);
startActivity(sendIntent);
這是打開消息應用程序或視頻群聊的示例代碼。 您也可以為其他人這樣做。
如果您不想一遍又一遍地選擇“活動”(例如使用createChooser時),請嘗試以下操作:
public class Chooser extends Activity implements OnClickListener {
private static final int NUM = 6;
private static final CharSequence DEFAULT = "click for select the app, long click to clear it";
private Intent[] mIntents = new Intent[NUM];
private LinearLayout mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLayout = new LinearLayout(this);
mLayout.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < NUM; i++) {
Button b = new Button(this);
b.setTag(i);
b.setText(DEFAULT);
b.setOnClickListener(this);
registerForContextMenu(b);
mLayout.addView(b);
}
setContentView(mLayout);
}
private CharSequence getName(Intent intent) {
PackageManager mgr = getPackageManager();
ResolveInfo info = mgr.resolveActivity(intent, 0);
if (info != null) {
return info.loadLabel(mgr);
}
return intent.getComponent().getShortClassName();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
int itemId = (Integer) v.getTag();
if (mIntents[itemId] != null) {
menu.add(Menu.NONE, itemId, Menu.NONE, "Clear");
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int i = item.getItemId();
Button b = (Button) mLayout.getChildAt(i);
b.setText(DEFAULT);
mIntents[i] = null;
return super.onContextItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Button b = (Button) mLayout.getChildAt(requestCode);
b.setText(getName(data));
mIntents[requestCode] = data;
startActivity(data);
}
}
@Override
public void onClick(View v) {
int i = (Integer) v.getTag();
if (mIntents[i] == null) {
Intent intent = new Intent(Intent.ACTION_PICK_ACTIVITY);
Intent filter = new Intent(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_LAUNCHER);
intent.putExtra(Intent.EXTRA_INTENT, filter);
startActivityForResult(intent, i);
} else {
startActivity(mIntents[i]);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.