欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程實現(xiàn)讀取手機聯(lián)系人、撥號、發(fā)送短信及長按菜單操作方法實例小結

 更新時間:2015年10月30日 15:27:18   作者:_YMW  
這篇文章主要介紹了Android編程實現(xiàn)讀取手機聯(lián)系人、撥號、發(fā)送短信及長按菜單操作方法,以完整實例形式總結分析了Android編程實現(xiàn)讀取手機聯(lián)系人、撥號、發(fā)送短信及長按菜單等操作的相關技巧,需要的朋友可以參考下

本文實例講述了Android編程實現(xiàn)讀取手機聯(lián)系人、撥號、發(fā)送短信及長按菜單操作方法。分享給大家供大家參考,具體如下:

1.Andrid項目結構圖↓主要操作圖中紅色方框內的文件。

2.首先布局代碼如下

a, main.xml 程序運行的主界面,主要用ListView列表控件展示手機聯(lián)系人

<?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:background="@drawable/bg"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dip"
android:cacheColorHint="#00000000"
android:divider="@drawable/divider_horizontal_bright"
android:paddingRight="5dip" >
</ListView>
</LinearLayout>

b.list_item.xml ListView的列表項布局文件,相當于展示模版

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/photo"
android:paddingRight="2dip" />
<TextView
android:id="@+id/name"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:paddingTop="8dip"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff" />
<TextView
android:id="@+id/number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:paddingTop="8dip"
android:singleLine="false"
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>

c,phonedetails.xml 長按菜單顯示聯(lián)系人詳細布局界面,示例只做了跳轉展示

<?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" >
<TextView
android:id="@+id/ymw"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>

2.Java實現(xiàn)代碼如下

a,MainActivity.java 程序運行的入口文件

package com.example.myandroid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.ymw.details.Detail;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView listView = (ListView) findViewById(R.id.listView);
// 生成動態(tài)數(shù)組,加入數(shù)據(jù)
ArrayList<HashMap<String, Object>> listItem = fillMaps();
SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,
R.layout.list_item,
new String[] { "imgView", "name", "number" }, new int[] {
R.id.imgView, R.id.name, R.id.number });
listView.setAdapter(listItemAdapter);
// 添加單擊事件
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
HashMap<String, String> map = (HashMap<String, String>) listView
.getItemAtPosition(arg2);
String name = map.get("name");
Toast toast = Toast.makeText(getApplicationContext(), "第"
+ arg2 + "項" + name, Toast.LENGTH_LONG);
toast.show();
String phoneNum = map.get("number");
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
+ phoneNum));
startActivity(intent);
}
});
// 添加長按菜單
listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("長按菜單-ContextMenu");
menu.add(0, 0, 0, "查看詳細");
menu.add(0, 1, 0, "發(fā)送信息");
menu.add(0, 2, 0, "刪除聯(lián)系人");
}
});
}
public boolean onContextItemSelected(MenuItem item) {
// setTitle("點擊了長按菜單里面的第"+item.getItemId()+"個項目");
Toast.makeText(getApplicationContext(),
"選擇了" + item.getItemId() + item.getTitle() + "項",
Toast.LENGTH_LONG).show();
int id = item.getItemId();
// 查看詳細
if (id == 0) {
Intent intent = new Intent();
intent.putExtra("ymw", item.getTitle());
intent.setClass(MainActivity.this, Detail.class);
startActivity(intent);
}
// 發(fā)送短信
else if (id == 1) {
Uri uri = Uri.parse("smsto://18664599745");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "ymw-LOVE-yh");
startActivity(intent);
}
// 刪除聯(lián)系人
else if (id == 2) {
}
return super.onContextItemSelected(item);
}
// 獲取手機聯(lián)系人列表方法一
public ArrayList<HashMap<String, Object>> GetContects() {
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC");
if (cursor.moveToFirst()) {
int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
int nameColum = cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
do {
String contactId = cursor.getString(idColumn);
String disPlayNameString = cursor.getString(nameColum);
// 查看有多少電話號碼 沒有則返回為0
int phoneCount = cursor
.getInt(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (phoneCount > 0) {
// 獲得聯(lián)系人的電話號碼
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ "=" + contactId, null, null);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("imgView", R.drawable.ic_launcher);
map.put("name", disPlayNameString);
list.add(map);
}
} while (cursor.moveToNext());
if (cursor != null)
cursor.close();
}
return list;
}
// 獲取聯(lián)系人方法二
public ArrayList<HashMap<String, Object>> fillMaps() {
ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
ContentResolver cr = getContentResolver();
HashMap<String, ArrayList<String>> hashMap = new HashMap<String, ArrayList<String>>();
Cursor phone = cr.query(CommonDataKinds.Phone.CONTENT_URI,
new String[] { CommonDataKinds.Phone.CONTACT_ID,
CommonDataKinds.Phone.DISPLAY_NAME,
CommonDataKinds.Phone.NUMBER,
CommonDataKinds.Phone.DATA1
// CommonDataKinds.StructuredPostal.DATA3,
}, null, null, null);
while (phone.moveToNext()) {
String contactId = phone.getString(phone
.getColumnIndex(CommonDataKinds.Phone.CONTACT_ID));
String displayName = phone.getString(phone
.getColumnIndex(CommonDataKinds.Phone.DISPLAY_NAME));
String PhoneNumber = phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String address = phone.getString(phone
.getColumnIndex(CommonDataKinds.Phone.DATA1));
// 以contactId為主鍵,把同一人的所有電話都存到一起。
ArrayList<String> ad = hashMap.get(contactId);
if (ad == null) {
ad = new ArrayList<String>();
ad.add(displayName);
ad.add(PhoneNumber);
// ad.add(address);
hashMap.put(contactId, ad);
} else {
ad.add(PhoneNumber);
}
}
phone.close();
ArrayList<String> tmpList;
String tmpStr = "";
int k;
Iterator iter = hashMap.entrySet().iterator();
while (iter.hasNext()) {
HashMap.Entry entry = (HashMap.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
tmpList = (ArrayList) val;
tmpStr = "";
for (k = 1; k < tmpList.size(); k++) {
tmpStr = tmpStr + tmpList.get(k) + ',';
}
tmpStr = GetString(tmpStr);
HashMap<String, Object> tmpMap = new HashMap<String, Object>();
tmpMap.put("name", tmpList.get(0));
tmpMap.put("number", tmpStr);
tmpMap.put("imgView", R.drawable.ic_launcher);
items.add(tmpMap);
}
return items;
}
private String GetString(String str) {
String strLast = "";
int i = str.lastIndexOf(",");
if (i > 0) {
strLast = str.substring(0, str.length() - 1);
}
return strLast.replace(" ", "").replace(",", "\n").replace("+86", "");
}
}

b,Detail.java 主界面長按菜單顯示聯(lián)系人詳細的跳轉界面,接受主界面?zhèn)鱽淼膮?shù)

package com.ymw.details;
import com.example.myandroid.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Detail extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(com.example.myandroid.R.layout.phonedetails);
Intent intent = getIntent();
String strPara = intent.getStringExtra("ymw");
TextView tView = (TextView) findViewById(R.id.ymw);
tView.setText(strPara);
}
}

3.獲取手機聯(lián)系人和撥號發(fā)短信等需要配置權限

在AndroidManifest.xml文件中的application節(jié)點上加入如下代碼

<!--添加權限-->
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>

4.使用Android模擬器或連接Android智能手機運行本程序可以看到手機聯(lián)系人列表,

單擊某個聯(lián)系人會直接撥號,長按某個聯(lián)系人會出現(xiàn)菜單選項,可以選擇發(fā)送短信。

希望本文所述對大家Android程序設計有所幫助。

相關文章

  • Android實現(xiàn)平滑翻動效果

    Android實現(xiàn)平滑翻動效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)平滑翻動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Android RecyclerView實現(xiàn)數(shù)據(jù)列表展示效果

    Android RecyclerView實現(xiàn)數(shù)據(jù)列表展示效果

    這篇文章主要為大家詳細介紹了Android RecyclerView實現(xiàn)數(shù)據(jù)列表展示效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 詳解Android Bitmap的常用壓縮方式

    詳解Android Bitmap的常用壓縮方式

    這篇文章主要介紹了詳解Android Bitmap的常用壓縮方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Android編程實現(xiàn)類似天氣預報圖文字幕垂直滾動效果的方法

    Android編程實現(xiàn)類似天氣預報圖文字幕垂直滾動效果的方法

    這篇文章主要介紹了Android編程實現(xiàn)類似天氣預報圖文字幕垂直滾動效果的方法,涉及Android基于布局及事件響應實現(xiàn)圖文滾動效果的相關操作技巧,需要的朋友可以參考下
    2017-08-08
  • android里面屏蔽home鍵/禁止Home鍵或者隨你DIY

    android里面屏蔽home鍵/禁止Home鍵或者隨你DIY

    可以先禁止Home鍵,再在onKeyDown里處理按鍵值,點然后在擊Home鍵的時候就把程序關閉,或者隨你DIY等等,感覺你可以隨心所欲吧,再接再厲,希望本文可以幫助到你
    2013-01-01
  • Android實現(xiàn)朋友圈多圖顯示功能

    Android實現(xiàn)朋友圈多圖顯示功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)朋友圈多圖顯示功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android實現(xiàn)系統(tǒng)打印功能

    Android實現(xiàn)系統(tǒng)打印功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)系統(tǒng)打印功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Android使用Toast顯示消息提示框

    Android使用Toast顯示消息提示框

    這篇文章主要為大家詳細介紹了Android使用Toast顯示消息提示框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android Picasso使用高斯模糊處理的示例代碼

    Android Picasso使用高斯模糊處理的示例代碼

    本篇文章主要介紹了Android Picasso使用高斯模糊處理的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Android完全退出應用程序的方法

    Android完全退出應用程序的方法

    這篇文章主要介紹了Android完全退出應用程序的方法,實例分析了Android退出應用程序的相關方法與實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09

最新評論