1. 程式人生 > >Android-上下文選單Menu

Android-上下文選單Menu

上一篇部落格介紹了,Android-普通選單Menu,而這篇部落格介紹Android-上下文選單Menu

 

AndroidManifest.xml 中加入許可權:

   <!-- 讀取聯絡人資料的許可權 -->
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    
    <!-- 讀取通話記錄的全身 -->
    <uses-permission android:name="android.permission.READ_CALL_LOG"
/> <!-- 修改通話記錄的許可權 --> <uses-permission android:name="android.permission.WRITE_CALL_LOG" />

 

Activity

package liudeli.activity;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.CallLog; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import
android.view.ViewGroup; import android.widget.AdapterView; import android.widget.CursorAdapter; import android.widget.ListView; import android.widget.TextView; public class ContentMenuActivity extends Activity { private static final int MENU_ITEM_COPY_TO_DIALER = Menu.FIRST; private static final int MENU_ITEM_SEND_SMS = Menu.FIRST + 1; private static final int MENU_ITEM_COPY_TO_CLIPBOARD = Menu.FIRST + 2; private CursorAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_content_menu); // 初始化控制元件 ListView lv = findViewById(R.id.lv); // 獲取通話記錄的資料 ContentResolver cr = getContentResolver(); Uri uri = CallLog.Calls.CONTENT_URI; String[] projection = new String[]{CallLog.Calls._ID, CallLog.Calls.CACHED_NAME, CallLog.Calls.NUMBER, CallLog.Calls.TYPE}; Cursor cursor = cr.query(uri, projection, null, null, null); adapter = new MyAdapter(this, cursor); lv.setAdapter(adapter); /** * 1 給listview註冊上下文選單 */ registerForContextMenu(lv); } /** * 2 建立上下文選單 * @param menu * @param v * @param menuInfo */ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add(0, MENU_ITEM_COPY_TO_DIALER, 0, "複製號碼到撥號盤"); menu.add(0, MENU_ITEM_SEND_SMS, 0, "傳送簡訊"); menu.add(0, MENU_ITEM_COPY_TO_CLIPBOARD, 0, "複製號碼到貼上板"); super.onCreateContextMenu(menu, v, menuInfo); } /** * 3 處理上下文選單的點選事件 * @param item * @return */ @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); int position = acmi.position; Cursor cursor = (Cursor) adapter.getItem(position); String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)); Intent intent; int id = item.getItemId(); switch (id) { case MENU_ITEM_COPY_TO_DIALER: //啟用撥號盤的元件,並且把號碼傳遞過去 intent = new Intent(); intent.setAction(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:"+number)); startActivity(intent); break; case MENU_ITEM_SEND_SMS: intent = new Intent(); intent.setAction(Intent.ACTION_SENDTO); intent.setData(Uri.parse("smsto:"+number)); startActivity(intent); break; case MENU_ITEM_COPY_TO_CLIPBOARD: //剪貼簿是一個系統服務 ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); //複製資料 clipboardManager.setText(number); break; default: break; } return super.onContextItemSelected(item); } private class MyAdapter extends CursorAdapter { private LayoutInflater mInflater; public MyAdapter(Context context, Cursor c) { super(context, c); mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { View view = mInflater.inflate(R.layout.lv_item, null); return view; } @Override public void bindView(View view, Context context, Cursor cursor) { // 得到控制元件 TextView tv_name = view.findViewById(R.id.tv_name); TextView tv_number = view.findViewById(R.id.tv_number); TextView tv_type = view.findViewById(R.id.tv_type); // 得到資料 String name = cursor.getString(1); String number = cursor.getString(2); int type = cursor.getInt(3); // 繫結資料 if(name == null){ tv_name.setText("未知"); }else{ tv_name.setText(name); } tv_number.setText(number); switch (type) { case CallLog.Calls.INCOMING_TYPE: tv_type.setText("來電"); break; case CallLog.Calls.OUTGOING_TYPE: tv_type.setText("撥號"); break; case CallLog.Calls.MISSED_TYPE: tv_type.setText("未接"); break; default: break; } } } }

 

Activity的佈局檔案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

 

介面卡要使用的 item 佈局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView 
        android:id="@+id/tv_name"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
       
        />
    <TextView 
        android:id="@+id/tv_number"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
       
        />
    <TextView 
        android:id="@+id/tv_type"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
       
        />

</LinearLayout>

 

效果: