linux 机器人连安卓,Android开发教程:机器人的意图之Intent的Action

Intetn的Action属性:

什么是Action?

Action是指Intent是指一个要完成的动作,是一个字符串常量。在Intent类里面定义了大师的Action的常量属性,例如:ACTION_CALL (打电话)  ACTION_EDIT(编辑数据)等等,除此之外,我们可以使用setAction()函数来设置IntnetAction属性,使用getAction()来获得Action

1、  自定义Action:

再看Intent构造函数:

public Intent()

默认的构造函数

public Intent(Intent o)

通过一个创建好的Intent复制过来

public Intent(String action)

通过指定的动作创建Intent,至于一些常见的动作将在后面指出

public Intent(String action,Uri uri)

通过指定的action和uri创建一个Intent

public Intent(Context packageContext,Class> cls)

参数说明:

packageContext:应用程序上下文

cls:接收Intent的目标组件

public Intent(String action,Uri uri,Context packageContext, Class> cls)

参数说明:

action:指定的action 例如:ACTION_VIEW

packageContext:应用程序上下文

cls:接收Intent的目标组件

uri:指定的uri

启动Activity

1、  显示启动:必须指明启动的Activity所在的类

// 创建一个Intent  应用程序上下文  目标组件

Intent intent =newIntent(IntentDemo01Activity.this,

ActivityToStart.class);

//显示启动Activity

startActivity(intent);

示例代码:

packagecom.jiahui.activity;

importAndroid.app.Activity;

importandroid.content.Intent;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

publicclassIntentDemo01ActivityextendsActivity {

privateButton myBtn;

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

myBtn = (Button) findViewById(R.id.myBtn);

myBtn.setText("启动ActivtiToStart");

myBtn.setOnClickListener(newOnClickListener() {

publicvoidonClick(View v) {

// 创建一个Intent  应用程序上下文  目标组件

Intent intent =newIntent(IntentDemo01Activity.this,                                        ActivityToStart.class);

//显示启动Activity

startActivity(intent);

}

});

}

}

packagecom.jiahui.activity;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.widget.TextView;

publicclassActivityToStartextendsActivity {

@Override

protectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.start);

TextView myText=(TextView)findViewById(R.id.startTxt);

myText.setText("启动的Activity");

}

}

2、  隐式启动:由Android系统根据Intent的动作和数据来决定启动哪一个Activity,只包含需要执行的动作和所包含的数据,无需指明具体哪一个Activity

packagecom.jiahui.activity;

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.net.Uri;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

publicclassIntentDemo02ActivityextendsActivity {

privateButton myBtn;

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

myBtn = (Button) findViewById(R.id.myBtn);

myBtn.setText("隐式");

myBtn.setOnClickListener(newOnClickListener() {

publicvoidonClick(View v) {

// 浏览

// Intent intent = new Intent(Intent.ACTION_VIEW, Uri

// .parse("http://www.goole.com.cn"));

//

// 显示拨号盘

Intent intent =newIntent(Intent.ACTION_DIAL);

startActivity(intent);

}

});

}

}

综合实例1:

packagecom.jiahui.activity;

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.net.Uri;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.EditText;

publicclassIntentWebViewDemoActivityextendsActivity {

privateButton myBtn;

privateEditText myText;

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

myBtn = (Button) findViewById(R.id.myBtn);

myText = (EditText) findViewById(R.id.edtxt01);

myBtn.setText("浏览");

myBtn.setOnClickListener(newOnClickListener() {

@Override

publicvoidonClick(View v) {

String uriString = myText.getText().toString();

Intent intent =newIntent(Intent.ACTION_VIEW, Uri

.parse(uriString));

startActivity(intent);

}

});

}

}0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值