从应用中拨打联系人/现有号码

时间:2014-09-09 11:08:29

标签: android permissions call contacts

我正在为Android应用程序编程,我想从这个应用程序中调用一个随机电话号码...所以我得到了这个,你可以输入一个联系人,他的号码将保存在sharedPreferences中..不知道但它看起来很酷:D ..现在我想调用用户添加的最新联系人,但我不知道该怎么做...我已经在我的程序中复制了很多其他问题的代码而没有成功.. 。

希望你能帮我添加一个通话功能

嗯...嗯我复制了几乎整个代码..所以不要问我请问某些陈述是什么......(我不会有任何反对言论的事情)

    package com.example.getcontacts;

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.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    protected static final String EXTRA_MESSAGE = "com.example.getcontacts";
    private TextView tv;
    private int i = loadI();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.tv);


        load(tv, i);
     // this opens the activity. note the  Intent.ACTION_GET_CONTENT
        // and the intent.setType
        ((Button)findViewById(R.id.pick_person)).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);
                startActivityForResult(intent, 1);                
            }
        });

        //get all saved phone numbers
        ((Button)findViewById(R.id.allNr)).setOnClickListener( new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent inte = new Intent(null, Contacts.class);
                inte.putExtra(EXTRA_MESSAGE, i);
                startActivity(inte);
            }
        });

    }

    //doing random sh*t which i dont know whaat happens
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            //get some datas... ?
            Uri uri = data.getData();

            if (uri != null) {
                //get more data
                Cursor c = null;
                try {
                    //initializing something
                    c = getContentResolver().query(uri, new String[]{ 
                                ContactsContract.CommonDataKinds.Phone.NUMBER,  
                                ContactsContract.CommonDataKinds.Phone.TYPE },
                            null, null, null);

                    if (c != null && c.moveToFirst()) {
                        //initializing phone number
                        String number = c.getString(0);
                        //get the type... somehow
                        int type = c.getInt(1);

                        //save in shared preferences and show the latest nr
                        int i = save(number);
                        showSelectedNumber(type, number);
                        load(tv, i);
                    }
                } finally {
                    if (c != null) {
                        c.close();
                    }
                }
            }
        }
    }

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



    @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);
    }

    public String load(TextView tv, int i) {
        String sharedNumber;
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(this);

        String c = Integer.toString(i);
        sharedNumber = sp.getString(c, "");
        tv.setText("Number: " + sharedNumber);
        return sharedNumber;
    }

    public int save(String value) {
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(this);
        Editor edit = sp.edit();
        int i = 0;
        String c;
        c = Integer.toString(i);
        edit.putString(c, value);
        edit.commit();
        i  =Integer.parseInt(c);
        i++;
        saveI("savedI", i);
        return i;
    }

    public void saveI(String key, int value) {
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(this);
        Editor edit = sp.edit();
        edit.putInt(key, value);
        edit.commit();
        }

    public int loadI() {
        int sharedNumber;
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(this);
        sharedNumber = sp.getInt("savedI", 0);
        return sharedNumber;
    }


}

0 个答案:

没有答案