效果图:

上下文菜单(复制号码、复制号码到短信界面、复制号码到拨号盘功能的实现)_android


代码实现:

1、main.xml


<?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="vertical" >
    
    <ListView 
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

</LinearLayout>




2、MainActivity

里面包括了读取通话记录中一些表的URI、以及打电话、发短信的SCHEMA,要是忘了的话,可以在可以查一下

但建议还是到android中去查


package com.njupt.contextmenu1;

import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog.Calls;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.text.ClipboardManager;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private static final int MENU_DIAL_ID = 0;
	private static final int MENU_SMS_ID = 1;
	private static final int MENU_COPY_ID = 2;
	
	private MyAdapter adapter;
	private ListView listview;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	    
		listview = (ListView) findViewById(R.id.listview);
		Cursor c = getContentResolver().query(Calls.CONTENT_URI ,new String[]{Calls._ID,Calls.NUMBER}, null,null, null);
		
		adapter = new MyAdapter(this,c);
		listview.setAdapter(adapter);
		registerForContextMenu(listview);
	}

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
    		ContextMenuInfo menuInfo) {
    	menu.add(0,MENU_DIAL_ID,0,"复制号码到拨号盘");
    	menu.add(0,MENU_SMS_ID,0,"发送短信");
    	menu.add(0,MENU_COPY_ID,0,"复制号码");
    	
    	super.onCreateContextMenu(menu, v, menuInfo);
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    	AdapterContextMenuInfo acmi =  (AdapterContextMenuInfo) item.getMenuInfo();
    	int position = acmi.position;
    	Cursor cursor = (Cursor) adapter.getItem(position);
    	String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
    	int id = item.getItemId();
         
    	Intent intent = null;
    	switch (id) {
		case MENU_DIAL_ID:
			intent = new Intent();
			intent.setAction(Intent.ACTION_DIAL);
			intent.setData(Uri.parse("tel:" + number));
			startActivity(intent);
			break;
		case MENU_SMS_ID:
			intent = new Intent();
			intent.setAction(Intent.ACTION_SENDTO);
			intent.setData(Uri.parse("smsto:" + number));
			startActivity(intent);
			break;
		case MENU_COPY_ID:
			ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
			cm.setText(number);
		default:
			break;
		}
    	
    	return super.onContextItemSelected(item);
    }
    
    private class MyAdapter extends CursorAdapter{
    	public MyAdapter(Context context , Cursor cursor) {
    		super(context, cursor);
		}
    	
    	@Override
    	public View newView(Context context, Cursor cursor, ViewGroup parent) {
    		
    		TextView tv = new TextView(context);
    		tv.setTextSize(22);
    		tv.setTextColor(Color.GREEN);
    		return tv;
    	}
    	
    	
    	@Override
    	public void bindView(View view, Context context, Cursor cursor) {
    		
    		String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
    		TextView tv = (TextView) view;
    		tv.setText(number);
    	}
    }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}




3、AndroidManifest.xml


<uses-permission android:name="android.permission.READ_CONTACTS"/>