android保存联系人并调用它们

时间:2014-09-15 07:50:10

标签: android call contacts

我正在编程一点点android,我在添加和调用联系人方面遇到了问题。

我的想法是添加两个联系人,当按下呼叫按钮时,用户可以决定他要拨打哪一个..我的两个问题是,在我的AVD上,代码正常工作,直到我必须打电话给某人。所以我可以选择2个联系人,这些联系人已经安全...在我的朋友的电话上我甚至无法选择联系人...程序只是说:“应用程序Contacts_SPrefs停止工作”...

我认为我的代码中存在问题..但我不知道它在哪里以及我如何解决它,因为在我以前的版本中代码有效...

因此,在应用程序的这一部分中,您可以添加2个联系人并选择按钮调用,这将打开下面描述的另一个意图

主要活动

package com.example.contact_sprefs;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

private String contact;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // this opens the activity. note the  Intent.ACTION_GET_CONTENT
    // and the intent.setType
    ((Button)findViewById(R.id.bro1)).setOnClickListener( new OnClickListener() {
        @Override
        public void onClick(View v) {
            // user BoD suggests using Intent.ACTION_PICK instead of .ACTION_GET_CONTENT to avoid the chooser
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            // BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
            intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            //-------- > this thing here opens the contacts view < ------
            //later its waiting until the user clicks on a number of his contacts
            contact = "Brah1";
            startActivityForResult(intent, 1);                
        }
    });

    ((Button)findViewById(R.id.bro2)).setOnClickListener( new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            contact = "Brah2";
            startActivityForResult(intent, 1);
        }
    });

    ((Button)findViewById(R.id.call)).setOnClickListener( new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), OpenMenu.class);
            startActivity(intent);
        }
    });        
}

//doing random sh*t which i dont know whaat happens
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data != null) {
        //after he clicked a number its getting to this point
        //in the uri it saves some number and all this things where the contact is
        //so this is for the location
        Uri uri = data.getData();

        if (uri != null) {
            //get more data
            //in this cursor all the values of the contacts are going to be safed
            //c is for the contact
            Cursor c = null;
            try {
                //here all datas from the contact are going to be safed in c
                c = getContentResolver().query(uri, new String[]{ 
                            ContactsContract.CommonDataKinds.Phone.NUMBER,  
                            ContactsContract.CommonDataKinds.Phone.TYPE  },
                        null, null, null);

                if (c != null && c.moveToFirst()) {
                    //c is the phone number in very special code types and stuff like that
                    //in c is everything from the contact saved
                    //now c is going to be converted into a string -> everything that stays is the phone number
                    String number = c.getString(0);
                    //get the type... somehow
                    int type = c.getInt(1);

                    showSelectedNumber(type, number);
                    save(contact, number);
                }
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

public void showSelectedNumber(int type, String number) {
    Toast.makeText(this, "Saved Nr : " + number, Toast.LENGTH_LONG).show();      
}

public void save(String type, String number) {
    SharedPreferences sp = PreferenceManager
            .getDefaultSharedPreferences(this);
    Editor edit = sp.edit();
    edit.putString(type, number);
    edit.commit();
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}    

}

所以...你可以看到..我真的不太了解Intent及其规格的主题..无论如何......继承人应用程序的第二部分,其中call-command是..和这个命令做任务...

OpenMenu

       package com.example.contact_sprefs;

    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.net.Uri;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    public class OpenMenu extends Activity {

        private String nr;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.open_menu);

            ((Button)findViewById(R.id.bro1)).setOnClickListener( new OnClickListener() {
                @Override
                public void onClick(View v) {
                    nr = load("Brah1", nr);
                    String uri = "tel:" + nr.trim() ;
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    intent.setData(Uri.parse(uri));
                    startActivity(intent);  
                }
            });

            ((Button)findViewById(R.id.bro2)).setOnClickListener( new OnClickListener() {
                @Override
                public void onClick(View v) {
                    nr = load("Brah2", nr);
                    String uri = "tel:" + nr.trim() ;
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    intent.setData(Uri.parse(uri));
                    startActivity(intent);

                }
            });
     }

        public void showSelectedNumber(String number) {
            Toast.makeText(this, "Took Nr : " + number, Toast.LENGTH_LONG).show();      
        }

        public String load(String brahNr, String number) {
            String sharedNumber;
            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(this);
            sharedNumber = sp.getString(brahNr, number);

            return sharedNumber;
        }



        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.get

ItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }    
}

....嗯......这是很多代码(对我而言)..我感谢所有帮助我的人:)


后来编辑

我忘了权限......所以这也需要

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />

VZ

0 个答案:

没有答案