安卓学习-Intent

例子1

定义一个activity的active,category,并调用

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.zzzzzzzzz"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity2"
            android:label="@string/title_activity_activity2" >
            <intent-filter >
                <action android:name="aaaaaaaaaaa"/>
                <action android:name="bbbbbbbbbbbbbb"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="ccc"/> 
            </intent-filter>
        </activity>
    </application>

</manifest>
View Code

调用代码

        Button btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction("aaaaaaaaaaa");
                intent.addCategory("ccc");
                startActivity(intent);
            }
        });
View Code

显示第二个窗体代码

        TextView v1=(TextView)findViewById(R.id.textView1);
        TextView v2=(TextView)findViewById(R.id.textView2);
        v1.setText("active:"+getIntent().getAction());
        Set<String> sets=getIntent().getCategories();
        v2.setText("category:"+sets+"");
View Code

 

例子2

查看并显示联系人电话

  

 

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent i=new Intent();
                i.setAction(Intent.ACTION_GET_CONTENT);
                i.setType("vnd.android.cursor.item/phone");
                startActivityForResult(i, 0);        
            }
        });
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO 自动生成的方法存根
        super.onActivityResult(requestCode, resultCode, data);
        
        if((requestCode==0)&&(resultCode==Activity.RESULT_OK)){
            Uri uri=data.getData();
            
            CursorLoader loader=new CursorLoader(this, uri, null, null, null, null);
            Cursor cursor=loader.loadInBackground();
            cursor.moveToFirst();
            
            String id = cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactsContract.Contacts._ID));
            String name = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

            Cursor phone = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
                            + id, null, null);// 第一个参数是确定查询电话号,第三个参数是查询具体某个人的过滤值
            String phones = "";
            // 一个人可能有几个号码
            while (phone.moveToNext()) {
                String strPhoneNumber = phone
                        .getString(phone
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                phones += (strPhoneNumber);
            }
            phone.close();
            
            TextView textView3=(TextView)findViewById(R.id.textView3);
            textView3.setText(name);
            TextView textView4=(TextView)findViewById(R.id.textView4);
            textView4.setText(phones);
             
        }
    }
}
View Code

 

 

例子3

返回主界面

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent i=new Intent();
                i.setAction(Intent.ACTION_MAIN);
                i.addCategory(Intent.CATEGORY_HOME);
                startActivity(i);        
            }
        });
    }

}
View Code

 

posted on 2014-12-26 23:03  weijj  阅读(184)  评论(0编辑  收藏  举报

导航