2011-11-02 52 views
0

我是Android新手。我想通過按下TextView來添加一個菜單。菜單應該有呼叫&消息& MMS選項。
我已經使用下面的代碼成功地實現了這個電子郵件,我想以類似的方式實現我的應用程序的呼叫,短信和彩信選項。意向創建呼叫和短信菜單選項.Action_send

tv4.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent i = new Intent(android.content.Intent.ACTION_SEND); 

       i.setType("html/plain"); 
       i.putExtra(Intent.EXTRA_EMAIL , new String[]{bean.getOfficialemailid()}); 
       i.putExtra(Intent.EXTRA_SUBJECT, "Official"); 
       i.putExtra(Intent.EXTRA_TEXT , "Have a Great Day"); 
       try { 
        startActivity(Intent.createChooser(i, "SEND EMAIL")); 
       } catch (android.content.ActivityNotFoundException ex) { 
        Toast.makeText(Database_display_activity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 

       } 
      } 
     }); 

回答

0

我找到了一個更好的方法來做到這一點。我打電話時按下菜單時通過的值:

tv3.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
     // TODO Auto-generated method stub 
     builder.setItems(R.array.OPTIONS, new DialogInterface.OnClickListener() { 
     @Override 
    public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
     switch (which) { 
     case 0:{ 
      Intent callIntent = new Intent(Intent.ACTION_CALL); 
      callIntent.setData(Uri.parse("tel:" + bean.getPhoneno())); 
      startActivity(callIntent); 
     }break; 
     case 1 :{ 

     }break; 
     } 

       } 
      }); 
      builder.show(); 
     } 
    }); 
1

希望你,這是你在找什麼:

private static final int SMS = 0; 
private static final int MMS = 1; 
private static final int CALL = 2; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

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

textView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
     ContextMenuInfo menuInfo) { 
    menu.add(0, 0, 0, "SMS"); 
    menu.add(0, 1, 1, "MMS"); 
    menu.add(0, 2, 2, "Call"); 
    } 
}); 

textView.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
    openContextMenu(v); 
    } 
}); 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) { 
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item 
.getMenuInfo(); 

switch (item.getItemId()) { 

case SMS: 
    //Code for sending SMS 

    break; 

case MMS: 
    //Code for sending MMS 

    break; 

case CALL: 
    Intent callIntent = new Intent(Intent.ACTION_CALL); 
    callIntent.setData(Uri.parse("tel:" + phoneNumber)); 
    startActivity(callIntent); 

    break; 


default: 
    return super.onContextItemSelected(item); 
} 

return true; 
} 

有關發送MMS幫助檢查此鏈接: www.coderanch.com/ t/453906/Android/Mobile/send-MMS

在onClick e上打開一個上下文菜單textView的發泄。

+0

嘿艾米謝謝你的回覆。使用此代碼時出現此錯誤。11-03 09:59:27.730:錯誤/ AndroidRuntime(1877):android.content.ActivityNotFoundException:未找到處理Intent的活動{act = android.intent.action.CALL dat = + 91-9711347489} – Vinay

+0

嘿艾米謝謝你的幫助,我有一個更好的辦法。你怎麼看待這個問題:-) – Vinay

+0

這個例外是因爲你沒有在manifest.xml中添加權限** **無論如何,很高興聽到你有你的解決方案。 – Ian