[英]onActivityResult is not called in android
我正在制作一個移動應用程序,在這里我想從聯系人那里獲取一個電話號碼。 當我單擊聯系人圖像視圖時,然后打開移動聯系人,然后選擇一個數字。之后,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) {
}
};
}
試試這個。
@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);
您是否在AndroidManifest中設置了權限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.