Pro Android学习笔记(十):了解Intent(上)

Android引入了Intent的概念来唤起components,component包括:
1、Activity(UI元件)
2、Service(后台代码)
3、Broadcast receiver(处理广播消息的代码)
4、Content provider(抽象数据的代码)

Intent基本含义

intent是通知平台处理(唤起)的动作。Android唤起的动作将取决于注册了什么动作。例如我们有个简单的Activity:IntentBaiscViewActivity。在AndroidManifest中,我们注册该动作,允许其他应用唤起该Activity。

<activity android:name=".IntentBasicViewActivity" android:label="@string/intent_basic_test">
    <intent-filter>
     <!-- 注册的action的名字为.intent.action.YOUR_ACTION_NAME。(试验:如果不采用package-name,调用时会出现找不到Activity的异常)  -->
        <action android:name="cn.xxxxxxx.android.pro.intent.action.TestIntentBasicView"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

采用以下方式进行调用:

String actionName = "cn.xxxxxxx.android.pro.intent.action.TestIntentBasicView";
Intent i = new Intent(actionName);
showInfo("invoke intent : \n\t" + i);
this.startActivity(i);

在被唤起的IntentBaiscViewActivity,可以找到唤起他的intent对象,如下:

Intent i = this.getIntent();
showInfo("invoke intent : \n\t" + i.toString() );

showInfo()是在textView中显示信息,本例子如下

系统的Intent

可以通过intent来调用Android系统的某些应用,具体的使用方式见http://developer.android.com/guide/appendix/g-app-intents.html。下面的小例子将试验这些调用。在这个例子中使用OptionMenu来选择具体的调用应用。Android的layout资源下有一个menu目录,我们在之下建议一个XML文件,也列举menu的group和item,当然我们也可以在代码中编写(可参考:Android学习笔记(八):Activity-OpenMenu和LinearLayout)。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <group android:id="@+id/intentMenu_MAIN">
        <item android:id="@+id/intent_menu_browser" android:title="
@string/intent_menu_browser"  />
        <item android:id="@+id/intent_menu_search" android:title="@string/intent_menu_search"  />
        <item android:id="@+id/intent_menu_dial" android:title="@string/intent_menu_dial"  />
        <item android:id="@+id/intent_menu_call" android:title="@string/intent_menu_call"  />
        <item android:id="@+id/intent_menu_map" android:title="@string/intent_menu_map"  />
    </group>
</menu>

Java代码如下:

public class IntentTestDemo extends Activity{
    … …        
    protected void onCreate(Bundle savedInstanceState) { 
         ……
    }
     
    
  //【Menu】1、创建OptionMenu需要重写onCreateOptionsMenu,再此通过MenuInflater从xml资源获取信息并构建菜单
    public boolean onCreateOptionsMenu(Menu menu) { 
        super.onCreateOptionsMenu(menu);
       MenuInflater mi = getMenuInflater();
        mi.inflate
(R.menu.intent_test_menu, menu);
        return true;
    } 

    // 【Menu】2、选择用户选择某菜单后触发onOptionsItemtSelected(),本例我们根据不同选择,出发不同的系统应用。 
    public boolean onOptionsItemSelected(MenuItem item) { 
        try{
            switch(item.getItemId()){
            case R.id.intent_menu_browser:  //对应xml中的id
                invokeBrowser();
                break;
            case R.id.intent_menu_search: 
                invokeSearch();
                break;
            case R.id.intent_menu_dial: 
                invokeDial();
                break;
            case R.id.intent_menu_call: 
                invokeCall();
                break;
            case R.id.intent_menu_map: 
                invokeMap();
                break;
            default: 
                Log.d("pro","get option error.");
                break;
            }
        }catch(Exception e){ 
            Log.d("pro",e.toString());
        }

        return true;
    }
   //【intent】触发浏览器打开指定网页,对于ACTION_VIEW,系统将根据传递的URI的格式,调用不同的应用,http://,https://时调用browser
    private void invokeBrowser(){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.google.com.hk"));
        startActivity(intent);
    }     
   //【intent】实际也是触发浏览器,不过名曰网络搜索,和上面ACTION_VIEW不同,运行传递的data为””(empty string),因为ACTION_VIEW对应多个可能的应用,而WEB_SEARCH只对应browser。
    private void invokeSearch(){
        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
        intent.setData(Uri.parse("http://www.google.com.hk"));
        startActivity(intent);
    }
  //【intent】打开拨号盘UI,可以预设tel:phone_number,或者voicemail:xxxxx。
    private void invokeDial(){
        Intent intent = new Intent(Intent.ACTION_DIAL);
        startActivity(intent);       
    }
    //【intent】打开拨号盘UI,同时呼出tel:phone_number,和ACTION_DIAL的需要用户按拨打键不同,ACTION_CALL直接呼出,需要授权android.permission_CALL_PHONE
    private void invokeCall(){ 
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:02012345678"));
        startActivity(intent);       
    }
    //【intent】给出地理位置,ACTION_VIEW选择并触发Google Map,前提是已经安装了Google Map。之前我们在模拟器上安装了华为的智慧云,有个应用商店比较方便,可以从那里直接安装。此外如果URI为google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom,VIEW将调用Google Streetview。
    private void invokeMap(){ 
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("geo:23.0,123.0")); 
        //intent.setData(Uri.parse("geo:23.0,123.0?z=4&q=business+near+city")); //增加zoom等参数
        startActivity(intent);
    } 

}

相关链接: 我的Android开发相关文章

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值