0

* *我试图做n sms应用程序并且我使用这个代码从联系人中挑选一些数据我的问题是当我关闭我的应用程序时联系人列表仍然打开..我怎样才能关闭它?

有什么方法可以选择数据并强制关闭联系人列表活动?

**

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnTouchListener{
    TextView a1,a2;
    EditText contact,message;
    Button send;
    String name,phoneNumber;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initilaize();
    }
    public void initilaize(){
        a1 = (TextView)findViewById(R.id.tVContact);
        a2 = (TextView)findViewById(R.id.tVMessage);
        contact = (EditText)findViewById(R.id.eTContact);
        message = (EditText)findViewById(R.id.eTMessage);
        send = (Button)findViewById(R.id.button1);
        name ="";
        phoneNumber="";

        contact.setOnTouchListener(this);
    }


    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);
            if (resultCode == Activity.RESULT_OK) {
                if (data != null) {
                    Uri contactData = data.getData();
                    try {
                        String id = contactData.getLastPathSegment();
                        Cursor phoneCur = getContentResolver()
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = ?", new String[] { id },
                                        null);

                        final ArrayList<String> phonesList = new ArrayList<String>();
                        while (phoneCur.moveToNext()) {
                            // This would allow you get several phone addresses
                            // if the phone addresses were stored in an array
                            String phone = phoneCur
                                    .getString(phoneCur
                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
                            phonesList.add(phone);
                        }
                        phoneCur.close();
                        //
                        String name="";
                        Cursor c =  managedQuery(contactData, null, null, null, null);  
                        if (c.moveToFirst()) {  
                            name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                        }
                        c.close();


                            //

                        if (phonesList.size() == 0) {

                        } else{
                           phoneNumber = phonesList.get(0);
                           this.name = name;
                           contact.setText(name);
                        }          } catch (Exception e) {
                        Log.e("FILES", "Failed to get phone data", e);

                     }
                }   

            }





    }
    public boolean onTouch1(View v, MotionEvent arg1) {
        switch(v.getId()){
        case R.id.eTContact:{
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
            startActivityForResult(intent,0);
            break;
        }
        case R.id.button1:{


        }
        }   
        return true;
    }
    public boolean onTouch(View arg0, MotionEvent arg1) {
        // TODO Auto-generated method stub
        return false;
    }

}
4

0 回答 0