zoukankan      html  css  js  c++  java
  • Intent(简单介绍)

       

    intent(意图),主要完成任务交接,当一个任务结束后,会将工作交给另一个任务,执行另一个任务。

        作用:1.activity方面。Intent对象传递给Context.starActivity()或Activity.starActivityForResult()可以启动一个新的activity.Activity的跳转,Activity的传值。

                2.Service方面。Context.startService()方法,启动新服务或者向正在运行的服务提供新命令。Intent对象传递到Context.bindSerivce()中将建立一个服务,建立组件间的联系。

                3.broadcastReceive方面。intent对象传递给Context.sendBroadcast等任何广播。他将传递给所有感兴趣的广播接收机。

      intent的组成部分: 

    Extras
        使用Intent连接不同组件时,有时需要在Intent中附加额外的信息,以便将数据传递给目标Activity。通常以Bundle的形式定义。
     
    Component
        指定Intent目标组件的名称。组件名称是一个ComponentName对象,这种对象名称是目标组件类名和目标组件所在应用程序的包名的组合。
     
    Type
        指定显示的MIME类型(与URI解析相对)
    Category
        对被请求的组件的额外描述信息。Android在Intent类中定义了一组静态常量便是Intent不同的类别。如:“android.intent.category.LAUNCHER”表示目标Activity是应用程序中最优先被执行的Activity。
     
    Data
        描述Intent要操作的数据。以URI形式表示的数据例如:content://contacts/1
     
    Action
        描述Intent所触发动作名字的字符串。例如:“android.intent.action.MAIN”表示程序的主入口,不会接受数据,结束后也不返回数据。
    下面打一个intent 页面跳转的小例子。
    第一个页面
    package com.dream;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class IntentShowActivity extends Activity {
        /** Called when the activity is first created. */
        private EditText nameET = null;
        private EditText passwordET = null;
        private Button btn = null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            nameET=(EditText)findViewById(R.id.MailET);
            passwordET=(EditText)findViewById(R.id.passwordET);
            btn=(Button)findViewById(R.id.Btn);
           btn.setOnClickListener(btnClick);
          /*  btn.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(IntentShowActivity.this,SecondActivity.class);
                    startActivity(intent);
                }
            });*/
            
        }
            OnClickListener btnClick = new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //定义变量,获取nameET,passwordET上的数据。
                String name = nameET.getText().toString().trim();
                String password = passwordET.getText().toString().trim();
                
                Intent intent = new Intent(IntentShowActivity.this,SecondActivity.class);
                 if(nameET!=null&&password!=null){
                     //把获取的数据一键值对的 形式存放在putExtra中
                     //将输入的值通过intent.putExtra进行封装,存放键值对的方式。
                     //键的名称,传递的内容。
                     intent.putExtra("nameET", name);
                     intent.putExtra("passwordET", password);
                }
                startActivity(intent);
                
            }
        };
    }

    第二个

    package com.dream;
    
    import android.app.Activity;
    import android.app.ActivityManager;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class SecondActivity extends Activity{
        /** Called when the activity is first created. */
        private TextView tv = null;
        private Button btnFinish = null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);
            tv=(TextView)findViewById(R.id.TV);
            btnFinish=(Button)findViewById(R.id.btnfinish);
            //定義Intent對象,
            Intent intent = getIntent();
            if(intent!=null){
                //定义变量,接受Intent传过来的键
                String name = intent.getStringExtra("nameET");
                String password = intent.getStringExtra("passwordET");
                //输出数据
                if(name!=null&&password!=null){
                    tv.setText("用户名:"+name+"密码:"+password);
                }
            }
        }
        public void btnfinish(View v){
            
            /*ActivityManager activityMgr= (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    
            activityMgr.restartPackage(getPackageName());
    */
    
            finish();
        }
    }

     
     
  • 相关阅读:
    数据库面试题
    MySQL表的导入
    MySQL表的导出
    MySQL安装mydumper
    MySQL中的日志
    动态数组实现下压栈
    动态数组
    设计模式之迭代器
    设计模式之组合模式
    设计模式之状态模式
  • 原文地址:https://www.cnblogs.com/LuckStarShine/p/2592332.html
Copyright © 2011-2022 走看看