博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android:Intent
阅读量:6914 次
发布时间:2019-06-27

本文共 4848 字,大约阅读时间需要 16 分钟。

Intent intent = getIntent();

Intent intent2 = new Intent();

通常通过getIntent()方法获取到是非空的,即类似于:

intent:Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.menutest/.MainActivity }

intent2 是通过new 方式实例化的,肯定为null。

一般情况下我们需要用getIntent()方式来获取到本Activity的intent。

 

intent通常用于跳转,inent有个setAction()方法,官方文档的解释如下:

. . .setAction( action)

Set the general action to be performed.

Parameters:
action An action name, such as ACTION_VIEW. Application-specific actions should be prefixed with the vendor's package name.
Returns:
Returns the same Intent object, for chaining multiple calls into a single statement.
See Also:
getAction
实际上,设置action即,setAction(String action)这个方法通常是用来判断是点击了哪个按钮,然后来做相应的判断以及数据处理。
有如下场景,比如之前的那个记事本的应用实例里,
如何判断是点了添加新日记还是检测呢,那么就可以在public boolean onOptionsItemSelected(MenuItem item) {}这个方法里进行判断,如果点了添加新日记,那么我们就给新建一个intent0,给这个intent0绑定一个action即绑定一个字符串,这个所谓的action实际上就是一个字符串。具体代码如下:
1 public boolean onOptionsItemSelected(MenuItem item) { 2  3         switch (item.getItemId()) { 4         case MENU_ITEM_INSERT: 5  6             Intent intent0 = new Intent(this, MyDiaryEdit.class); 7             //设置action,跳到第二个页面的时候就通过这个action来判断点了哪个按钮 8             intent0.setAction(MyDiaryEdit.INSERT_TEXT_ACTION); 9             System.out.println("intent0已经设置了Action,");10             /*11              * getIntent():Return the intent that started this activity 12              * getIntent().getData(): Retrieve data this intent is operating on.13              * Returns,The URI of the data this intent is targeting or null.14              */15             intent0.setData(getIntent().getData());//intent设置URI16             System.out.println("Return the intent that started this activity,the intent:"+getIntent());17             startActivity(intent0);18             return true;19 20         case MENU_ITEM_EDIT:21 22             Intent intent1 = new Intent(this, MyDiaryEdit.class);23             intent1.setAction(MyDiaryEdit.EDIT_TEXT_ACTION);24             intent1.setData(item.getIntent().getData());25             startActivity(intent1);26             return true;27 28         case MENU_ITEM_DELETE:29             Uri uri = ContentUris.withAppendedId(getIntent().getData(),30                     getListView().getSelectedItemId());31             getContentResolver().delete(uri, null, null);32             rewindView();33         }34 35         return super.onOptionsItemSelected(item);36     }

 

第二个页面判断是点了哪个按钮的代码:

1 protected void onCreate(Bundle savedInstanceState) { 2         super.onCreate(savedInstanceState); 3         setTheme(android.R.style.Theme_Black); 4         //Return the intent that started this activity.  5         final Intent intent = getIntent(); 6         //获取intent里到底是绑定action字符串,下面用来判断是点了哪个按钮 7         final String action = intent.getAction(); 8         setContentView(R.layout.mydiaryedit);//添加对应的layout 9         System.out.println("intent.getAction():"+intent.getAction());10         myToast("intent.getAction():"+intent.getAction());11         TextView wyl_tv = (TextView) findViewById(R.id.wyl_txt);12         wyl_tv.setText("intent.getAction():"+intent.getAction()+";\n\n"13                 + " URI uri = intent.getData(): "+intent.getData()+";\n\n MyDiaryEdit.java的intent:"+getIntent());14         mTitleText = (EditText) findViewById(R.id.title);15         mBodyText = (EditText) findViewById(R.id.body);16 17         confirmButton = (Button) findViewById(R.id.confirm);18         modifyButton = (Button) findViewById(R.id.modified);19 20         if (EDIT_TEXT_ACTION.equals(action)) {21             /*22              * EDIT_TEXT_ACTION.equals(action),EDIT_TEXT_ACTION就是前一个23              * 页面中intent.setAction的时候设置的哪个字符串,这样就能够判断是不是点了新增日记的按钮了24              */25             mState = STATE_EDIT;26             mTitleText.setEnabled(false);//设置为不可编辑27             mBodyText.setEnabled(false);//设置不可编辑28             modifyButton.setVisibility(View.VISIBLE);29             mUri = intent.getData();30             mCursor = managedQuery(mUri, projection, null, null, null);31             mCursor.moveToFirst();32             String title = mCursor.getString(1);33             mTitleText.setTextKeepState(title);34             String body = mCursor.getString(2);35             mBodyText.setTextKeepState(body);36 37             setResult(RESULT_OK, new Intent(MyDiaryEdit.EDIT_TEXT_ACTION, mUri));38             setTitle("编辑日记");39         } else if (INSERT_TEXT_ACTION.equals(action)) {40             mState = STATE_INSERT;41             setTitle("新建日记");42         } else {43 44             Log.e(TAG, "no such action error");45             MyDiaryEdit.this.finish();46 47             return;48         }49 50         confirmButton.setOnClickListener(new MyBtnOnClickListen());51         modifyButton.setOnClickListener(new MyBtnOnClickListen());52 53     }

 

 

 

转载地址:http://rqncl.baihongyu.com/

你可能感兴趣的文章
Python 扩大领先优势 18 年数据科学&工具排名
查看>>
面向未来的前端数据流框架 - dob
查看>>
小程序 · 云开发
查看>>
Android技术总监应该干的那些事
查看>>
Kotlin的装饰者模式与源码扩展
查看>>
GoogleDeveloperDay 回顾
查看>>
关于Create React App不支持装饰器的终极无伤解决方案
查看>>
Node.js&NPM的安装与配置
查看>>
[译] 使用 Web Beacon API 记录活动
查看>>
一线城市房价的理性思考
查看>>
人人都能掌握的Java服务端性能优化方案
查看>>
Android入门第一关:Android四大组件
查看>>
记一次混淆算法逆向分析
查看>>
header的安全配置指南
查看>>
W3C CSS Transforms摘译
查看>>
Logo设计的简要可行步骤
查看>>
ES6之Set和Map
查看>>
动画-仿微博弹簧动画
查看>>
[译] 单向用户界面架构
查看>>
shell script
查看>>