2014-05-07 27 views
0

我很沮喪,我如何讓功能正常工作。 我一直在研究和環顧意向。使用意圖開始活動有問題

起初我以爲我說得對,但我錯了,我的意思是一些概述。 我用6個按鈕做了一個應用程序,所有這些打開不同的應用程序。

  1. 時鐘2.日曆3.瀏覽器4.消息5.電話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_message。

當啓動應用程序,當我點擊按鈕。它促使我在一個窗口中選擇應用程序。它每次選擇按鈕時都會運行應用程序。

但是,當我選擇另一個應用程序,它啓動聯繫人應用程序?並不讓我像以前那樣選擇它。我怎樣才能解決這個問題?我很確定我正在使用意圖函數錯誤。

謝謝你的時間。

請再次檢查代碼,那是我的修改過的那個沒有工作的,它只有一個意圖方法。我添加了我最初使用的代碼,讓我選擇它。這是意向和範疇。 (一個你可以看到現在)

+0

要明確調用頁面 – Barun

+0

我以前這樣做了,但我認爲這是如果用戶選擇他們啓動的內容,效果會更好,因爲當我將該方法稱爲Explicit Intent時,它只會啓動一個應用程序,如果它不存在,它會崩潰,我不擅長過濾事物。 – SmallVille

+0

每個按鈕上單擊調用頁面explicitly.It將工作完全 – Barun

回答

1

如果你不想一遍又一遍選擇活動再次(使用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]); 
     } 
    } 
} 
+0

什麼不清楚?它只有不到100行的代碼,大部分與UI相關,所以真正的事情是結果 – pskink

+0

開始活動謝謝,但它並沒有保存活動。它會打開,當你回到那裏時,但當你回到主要活動,然後再次點擊按鈕,它會重置。 – SmallVille

+0

確定:您必須將它保存在某處,請參閱Intent.toUri和Intent.getIntent方法 – pskink

1

喜用下面的代碼以打開聯繫人:

@SuppressWarnings("deprecation") 
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI); 
startActivity(intent); 
1
Intent sendIntent = new Intent(Intent.ACTION_VIEW); 
sendIntent.setData(Uri.parse("sms:")); 
sendIntent.putExtra("sms_body", urlToShare); 
startActivity(sendIntent); 

這是一個示例代碼來打開消息應用程序或視頻羣聊。你也可以這樣做。

+0

這會打開oem消息應用程序嗎?像xperia一樣? – SmallVille

+0

我沒有在xperia中測試它。但我認爲它會啓動 – Kamalone

+0

另外,我怎麼能得到這個啓動其他應用程序? – SmallVille