我的Android笔记--Intent下,印象中常用的用法

1,拨打电话

Intent intent = new Intent(Intent.ACTION_DIAL);
Uri uri = Uri.parse("tel:" + "1511690XXXX");
intent.setData(uri);
startActivity(intent);

2,发送短信

Uri uri = Uri.parse("smsto:1511690XXXX");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.putExtra("sms_body", "这是消息主体");
startActivity(intent);

  

3,获取短信验证码

在获取完验证码以后,注册一个广播接受者,Action是"android.provider.Telephony.SMS_RECEIVED",另外注意权限设置。

接收短信的时候注意内容长度,内容超过2条时可能会出现异常状况。另外对于广播接受者也要注册合适的接口回调交互数据。

在接收者的onReceive方法中处理数据。

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (ACTION_RECEIVER.equals(action)) {
        Object[] pdus = (Object[]) intent.getExtras().get("pdus");

        for (Object pdu : pdus) {
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
            String sender = smsMessage.getDisplayOriginatingAddress();
            //短信内容
            String content = smsMessage.getDisplayMessageBody();
            long date = smsMessage.getTimestampMillis();
            Date timeDate = new Date(date);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = simpleDateFormat.format(timeDate);

            //过滤不需要读取的短信的发送号码
            if ("10086".equals(sender) && receiverSMS != null) {
                receiverSMS.callBack(content);
                abortBroadcast();
                break;
            }
        }

    }
}  

4,使用Intent重启应用

final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

5,Intent之setFlags()

需要注意的是,Context也可以使用startActivity()方法启动一个Activity,但是必须在传递的intent上调用addFlags()方法,参数使用Intent. Intent.FLAG_ACTIVITY_NEW_TASK,否则会报异常。

有时候有这么一个需求,我们打开一堆Activity,从A跳到B,然后C,然后D,再D页面处理逻辑结束之前,BCD页面是不能finish的,但是D页面逻辑结束后需要清除BC,直接跳转回A页面,这个时候使用Intent的setFlags()方法传输特定的flags就可以完成需求了。

Intent intent = new Intent(this, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

对于flag这个参数有很多值,记录下我常用的。

 Intent.FlAG_ACTIVITY_CLEAR_TOP。文档中给的注释是:

If set, and the activity being launched is already running in the

current task, then instead of launching a new instance of that activity,

all of the other activities on top of it will be closed and this Intent

will be delivered to the (now on top) old activity as a new Intent.

<p>For example, consider a task consisting of the activities: A, B, C, D.

If D calls startActivity() with an Intent that resolves to the component

of activity B, then C and D will be finished and B receive the given

Intent, resulting in the stack now being: A, B.

<p>The currently running instance of activity B in the above example will

either receive the new intent you are starting here in its

onNewIntent() method, or be itself finished and restarted with the

new intent.  If it has declared its launch mode to be "multiple" (the

default) and you have not set {@link #FLAG_ACTIVITY_SINGLE_TOP} in

the same intent, then it will be finished and re-created; for all other

launch modes or if {@link #FLAG_ACTIVITY_SINGLE_TOP} is set then this

Intent will be delivered to the current instance‘s onNewIntent().

<p>This launch mode can also be used to good effect in conjunction with

{@link #FLAG_ACTIVITY_NEW_TASK}: if used to start the root activity

of a task, it will bring any currently running instance of that task

to the foreground, and then clear it to its root state.  This is

especially useful, for example, when launching an activity from the

notification manager.

<p>See <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">

Tasks and Back  Stack</a> for more information about tasks.

翻译过来是:启动的activity已经运行在当前的任务中,那么它不是启动一个新的activity,而是将所有在这个activity上面的其它同栈activity清除掉,这个意图将会被传递到久活动中作为一个新的意图。

例如,一个任务中存在:A,B,C,D四个页面。D调用包含这个flag的意图的statrtActivity()方法启动B页面,C和D被清楚掉,B会接收到传递的意图,栈中现在是A,B。

正在运行的前面的B_activity的实例可能会在他的onNewIntent()方法中接收一个新的Intent或者结束他本身以后接收新的意图重启。这两种处理方式和他本身的启动方式有关,如果是默认的multiple模式,那么会接收新Intent重启自身,如果是{@link #FLAG_ACTIVITY_SINGLE_TOP}的启动模式,这个新意图会被传递到当前实例activity的onNewIntent()方法中。

这个启动模式结合{@link #FLAG_ACTIVITY_NEW_TASK}使用也是很有效的:如果一个任务中使用它中启动一个根activity,它会将该任务的当前运行实例呈现在首页,然后明确其根状态。例如,当从通知管理器启动一个activity时是尤其有用的。

 

     Intent.FLAG_ACTIVITY_NO_HISTORY。这个flag使用起来简单一些。使用它启动的activity不会被压入栈中。例如,A,B。B通过这个flag打开C,C再打开D,但是D返回的时候不会经过C,直接回到B。

6,PendingIntent

使用PendingIntent需要注意的是它的getActivity(),getService()和getBrocast()方法,对于包装里的intent,有时对于类参数的设定,最好用Class.forName()来设置。

Intent还有很多用途,现在我只记录下还记得的,以后会逐渐添加的。

时间: 2024-08-04 00:28:59

我的Android笔记--Intent下,印象中常用的用法的相关文章

Anaconda中常用的用法

Anaconda中常用的用法 conda 是开源包(packages)和虚拟环境(environment)的管理系统. packages 管理: 可以使用 conda 来安装.更新 .卸载工具包 ,并且它更关注于数据科学相关的工具包.在安装 anaconda 时就预先集成了像 Numpy.Scipy. pandas.Scikit-learn 这些在数据分析中常用的包.另外值得一提的是,conda 并不仅仅管理Python的工具包,它也能安装非python的包.比如在新版的 Anaconda 中就

Android笔记 Intent用法总结

Intent: 官方解释: An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindSe

【学习笔记】——原生js中常用的四种循环方式

一.引言 本文主要是利用一个例子,讲一下原生js中常用的四种循环方式的使用与区别: 实现效果: 在网页中弹出框输入0   网页输出"欢迎下次光临" 在网页中弹出框输入1   网页输出"查询中--" 在网页中弹出框输入2   网页输出"取款中--" 在网页中弹出框输入3   网页输出"转账进行中--" 在网页中弹出框输入其他字符   网页输出"无效按键" 四种循环: for循环 while循环 for  in

关于在adb调试过程中常用的用法及adb命令

本篇主要讲解一下几种关于我们在调试过程中常用的几个adb命令及用法,adb连接的具体过程这里就不再多讲 关于怎么通过adb命令连接手机进行调试请参考 如何通过PC端连接Android设备进行adb调试 首先必须确保你正确配置了AndroidSDK(不然这里是直接打不开的,需要进入到SDK-platform-tools路径下进行adb命令),话不多说,看图 1) adb devices ( 用于查看当前adb连接的设备有哪些,这里目前是无) 2) adb connect IP地址 (用于连接And

android笔记--Intent和IntentFilter详解

Intent用于启动Activity, Service, 以及BroadcastReceiver三种组件, 同时还是组件之间通信的重要媒介. 使用Intent启动组件的优势1, Intent为组件的启动提供了一致的编程模型. 无论想要启动的组件是Activity, Service, 还是BroadcastReceiver, 都可以使用Intent封装启动的意图.2, 在某些时候, 应用程序只是想启动具有某种特征的组件, 并不想和某个特定的组件耦合. 使用Intent可以方便的达到这种高层次解耦的

我的Android笔记--Intent上(我是文档的搬运工)

我是文档的搬运工. Intent,意图,它是一个消息传递对象.对于意图最常见的就是从一个Activity跳转到另一个Activity,或者启动一个服务.发送一个广播等等. 它分为显式意图和隐式意图,下面是API指南给的解释. 显式 Intent :按名称(完全限定类名)指定要启动的组件.通常,您会在自己的应用中使用显式 Intent 来启动组件,这是因为您知道要启动的 Activity 或服务的类名.例如,启动新 Activity 以响应用户操作,或者启动服务以在后台下载文件. 隐式 Inten

【Android笔记】在一个Activity中结束另一个Activity

以在Activity_B中结束Activity_A为例: 1.在Activity_A中定义一个Acitivity的全局静态变量: 1 public static Activity actA; 2.在Activity_A的OnCreate()方法中给Activity_A赋值: 1 actA=this; 3.在Activity_B中,需要结束Activity_A时,调用以下: 1 ActivityA.actA.finish(); 另外也可用广播的方式实现(此处忽略了,需要的自己试下吧) 参考原文:h

android笔记5——同一个Activity中Fragment的切换

今天来模拟一个注冊的界面过程: 点击"下一步"之后: 说明一下:界面总局仅仅在一个Activity里面. 1.首先定义RegistActivity public class RegistActivity extends Activity { private EditText userEditText; private EditText verifyCodeText; private Fragment verifyCodeFragment; private Fragment checkC

Android笔记——在布局文件中插入另一个布局文件

假如有一个布局文件A.xml想把另外一个布局文件B.xml引进其布局,则可以通过下面的代码 <include layout="@layout/B" />