我正在开发一个Android应用程序,我想通过点击按钮阅读或获取Sim卡联系人。 你可以帮帮我吗 ?
我的按钮xml代码是:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/third"
android:layout_below="@+id/third"
android:layout_marginTop="30dp"
android:text="Read contact"
android:onClick="contactsAccess"/>
我需要在methode(contactsAccess)中使用java代码。
答案 0 :(得分:0)
public class MainActivity extends ActionBarActivity implements OnClickListener {
Button sim_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_VIEW);
sim_btn = (Button)findViewById(R.id.button1);
sim_btn.setOnClickListener(this);
getallsimnumber();
}
getallsimnumber(){
try
{
String ClsSimPhonename = null;
String ClsSimphoneNo = null;
Uri simUri = Uri.parse("content://icc/adn");
Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null);
Log.i("PhoneContact", "total: "+cursorSim.getCount());
while (cursorSim.moveToNext())
{
ClsSimPhonename =cursorSim.getString(cursorSim.getColumnIndex("name"));
ClsSimphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
ClsSimphoneNo.replaceAll("\\D","");
ClsSimphoneNo.replaceAll("&", "");
ClsSimPhonename=ClsSimPhonename.replace("|","");
Log.i("PhoneContact", "name: "+ClsSimPhonename+" phone: "+ClsSimphoneNo);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}