在当前程序中调用其他程序的Activity

来源:互联网 发布:柬埔寨网络博客推广 编辑:程序博客网 时间:2024/05/04 09:30

在当前程序中调用其他程序的Activity 

Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent作用的表现形式为:
1.通过Context.startActivity() or Activity.startActivityForResult() 启动一个Activity
2.通过 Context.startService() 启动一个服务,或者通过Context.bindService() 和后台服务交互;
3.通过广播方法(比如 

Context.sendBroadcast(),Context.sendOrderedBroadcast(),Context.sendStickyBroadcast()) 发给broadcast receivers

在代码中调用其他Activity我们可以通过intent的方式,通过限定参数格式的方式决定启用哪个注册了的Activity,一个利用intent实现文字共享的应用代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

public class ShareViaIntent extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button mButton1 = (Button) findViewById(R.id.renren);
        mButton1.setOnClickListener(mShareRenren);
        Button mButton2 = (Button) findViewById(R.id.gmail);
        mButton2.setOnClickListener(mShareGmail);
        Button mButton3 = (Button) findViewById(R.id.sms);
        mButton3.setOnClickListener(mShareSMS);

        edittext = (EditText) findViewById(R.id.edittext);
        edittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // Perform action on key press
                    Toast.makeText(ShareViaIntent.this, edittext.getText(),
                            Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        });
    };

    /*
     * 如果不对数据传输格式setType或者对附加信息puExtra作特殊说明,系统会自动罗列
     * 出一张经过android.content.Intent.ACTION_SEND过滤的程序表单,供用户选择
     * */
    private OnClickListener mShareRenren = new OnClickListener() {
        public void onClick(View v) {
            Intent share = new Intent(android.content.Intent.ACTION_SEND);
            share.setType("text/plain");                                //确定数据格式                           
            share.putExtra(Intent.EXTRA_SUBJECT"Title");    //添加附加信息
            share.putExtra(Intent.EXTRA_TEXT, edittext.getText().toString());
            startActivity(Intent.createChooser(share, "其他转发方式"));  
        }
    };
   
    /*对附加信息作了约束:
     * sendmail.putExtra(android.content.Intent.EXTRA_EMAIL,"default@wordsshare.com");  
     * sendmail.putExtra(android.content.Intent.EXTRA_TEXT, EmailBody);
     * 所以下述click事件对应的是发送邮件
     * */
    private OnClickListener mShareGmail = new OnClickListener() {
        public void onClick(View v) {
            Intent sendmail = new Intent(android.content.Intent.ACTION_SEND);
            sendmail.setType("plain/text");
            String EmailBody = edittext.getText().toString();
            sendmail.putExtra(android.content.Intent.EXTRA_EMAIL,
                     "default@wordsshare.com");
            sendmail.putExtra(android.content.Intent.EXTRA_TEXT, EmailBody);
            startActivity(Intent.createChooser(sendmail, getResources()
                    .getString(R.string.viaGmail)));
        }
    };

    /*对数据格式和附加信息作了约束:
     * sendSMS.putExtra("sms_body", edittext.getText().toString());    
     *      sendSMS.setType("vnd.android-dir/mms-sms");
      *所以下述click对应的事件是发送短信
      * */
    private OnClickListener mShareSMS= new OnClickListener() {
        public void onClick(View v) {
            Intent sendSMS = new Intent(Intent.ACTION_VIEW);    
            sendSMS.putExtra("sms_body", edittext.getText().toString());    
            sendSMS.setType("vnd.android-dir/mms-sms");    
            startActivity(sendSMS);
        }
    };
   
    public EditText edittext;
}

然而通过intent+component name的方式我们可以直接指定调用哪一个包或者程序的哪个Activity

1
2
3
4

Intent intent = new Intent(Intent.ACTION_MAIN);
ComponentName componentName = new ComponentName("包名""activity");
intent.setComponent(componentName);  
startActivity(intent);

点击查看更多关于intent机制的详细资料

原创粉丝点击