我正在制作移动应用,在这里我想从联系中取一个号码。当我点击联系人imageview然后移动联系人打开然后我选择一个数字。之后mob_no edittext是空白,这意味着onActivityResult没有被调用,我也打印结果代码,请求代码和数据的值,但它没有被执行。请解决我的问题如何调用onActivityResult(...)
public class Recharge_activity extends Activity{
private static final int PICK_CONTACT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
mob_no = (EditText) findViewById(R.id.mobilenum);
mob_no.requestFocus();
contact = (ImageView) findViewById(R.id.mobile_contact);
mob_no.addTextChangedListener(textWatcher);
contact.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try{
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, 1);
} catch(Exception e){
Toast.makeText(Recharge_activity.this, "Number is not format", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
String[] projection = {Phone.NUMBER};
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(contactData, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
String newNum = number.replaceAll("-", "").trim();
newNum=newNum.replace("+91", "");
mob_no.setText(newNum);
}
}
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//after text changed
if (count==0)
{
counter=counter-1;
}else if(count==1)
{
counter = count + counter;
}
if(counter!=0)
{
if(counter==6 && count==1)
{
spinner.setEnabled(true);
cont_no=mob_no.getText().toString();
setfillspinner();
}
if(counter<5)
{
provider_name.clear();
spinner.setEnabled(false);
provider_name.add("Select Operator");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(Recharge_activity.this
,android.R.layout.simple_spinner_item, provider_name);
System.out.println("value call double time");
// Set the Adapter
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//amt.setFocusable(true);
spinner.setAdapter(arrayAdapter);
spinner.setSelection(0);
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
}
答案 0 :(得分:0)
试试这个。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
//put your code here
}
super.onActivityResult(requestCode, resultCode, data);
}
编辑答案
Intent pickContactIntent = new Intent(MainActivity.this, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, 1);
答案 1 :(得分:0)
您是否在AndroidManifest中设置了权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>