Intent初级学习

发送短信的代码:在MainActivity.java中添加

Btn=(Button)findViewById(R.id.mainBtn);
        Btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_SENDTO);
                intent.setData(Uri.parse("smsto:110"));
                intent.putExtra("sms_body","Welcome to.....");
                startActivity(intent);
            }
        });
==============注意进行button的定义(private Button Btn=null;)=======================
在AndroidManifest.xml添加如下代码,设置发短信(和打电话)的代码
<uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
View Code

创建和Activity相互调用的多个Activity的相对应的操作:new-> other ->activity 可以创建出Activity和与其相关的xml文件。

在MainActivity中的button设置监听事件,设置如下代码调用刚才设置的Activity.

Intent intent=new Intent();
                intent.setClass(MainActivity.this, Thirdctivity.class); //Thirdctivity为创建Activity的名称
                startActivity(intent);
View Code

 在MainActivity中写入如下代码可以在Thirdctivity中更新TextView的内容。

Intent intent=new Intent();
                intent.setClass(MainActivity.this, Thirdctivity.class);
                intent.putExtra("str", "Intent Demo");
                startActivity(intent);
====================Thirdctivity====================
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thirdctivity);
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        String str=bundle.getString("str");
        thirdTxt=(TextView)findViewById(R.id.thirdTxt);
        thirdTxt.setText(str);
    }
View Code

接受Thirdctivity按键Button传送回来信息的代码:

//按键监听器
thirdBtn=(Button)findViewById(R.id.thirdBtn);
        thirdBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent();
                intent.putExtra("back", "Back Data");
                setResult(RESULT_CODE, intent);
                finish();
                
            }
        });
============MainActivity中编辑的代码================
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==REQUEST_CODE) {
            if(resultCode==Thirdctivity.RESULT_CODE) {
                Bundle bundle=data.getExtras();
                String str=bundle.getString("back");
                Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
            }
        }
    }
显示toast接受传送回来的文本信息
View Code

 

 

posted @ 2013-06-03 10:39  zhang1107  阅读(120)  评论(0编辑  收藏  举报