zoukankan      html  css  js  c++  java
  • Android-----Intent中通过startActivity(Intent intent )隐式启动新的Activity

    显式Intent我已经简单使用过了,也介绍过概念,现在来说一说隐式Intent:

    隐式Intent:就是只在Intent中设置要进行的动作,可以用setAction()和setData()来填入要执行的动作和数据,然后再用startActivity()启动合适的程序。

    此外:如果手机中有多个适合的程序,还会弹出列表供用户选择(假如你手机有两个浏览器,你打开一个连接,这是系统就会弹出两个浏览器列表供你选择)

    在此举个使用隐式Intent打开activity的快速拨号例子:

    xml布局文件代码如下:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     android:orientation="vertical"
     8     android:layout_gravity="center"
     9     tools:context="com.hs.example.exampleapplication.IntentFirst">
    10 
    11     <Button                  //简单做一个Button组件
    12         android:id="@+id/btn_call"
    13         android:layout_width="match_parent"
    14         android:layout_height="wrap_content"
    15         android:background="@color/colorPrimaryLight"
    16         android:textSize="18dp"
    17         android:textColor="#FFFFFF"
    18         android:text="打电话到10086"
    19         android:onClick="onClick"/>   //点击button是执行onClick方法,onClick方法在MainActivity中创建
    20 
    21 </LinearLayout>

    我们打电话需要在AndroidManifest.xml中加入静态申请的权限:

    但是Android 6.0之后,仅仅是在配置文件中获取权限是没有相应权限的,还有通过动态来获取权限,在下面的MainActivity.java中有说到

    1 <uses-permission android:name="android.permission.CALL_PHONE"/>

    MainActivity.java代码如下:

     1 public class IntentFirst extends AppCompatActivity {
     2 
     3     private static final int REQUEST_EXTERNAL_STORAGE = 1;
     4     private static String[] PERMISSIONS_STORAGE = {"android.permission.CALL_PHONE"};//要获取的权限,可以获取多个
     5   //此方法用于动态获取打电话权限
     6     public static void getCallPermissions(Activity activity) {
     7         try {
     8             //检测是否有呼叫的权限
     9             int permission = ActivityCompat.checkSelfPermission(activity,"android.permission.CALL_PHONE");
    10             if (permission != PackageManager.PERMISSION_GRANTED) {
    11                 // 没有呼叫的权限,去申请权限,会弹出对话框
    12                 ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
    13             }
    14         } catch (Exception e) {
    15             e.printStackTrace();
    16         }
    17     }
    18 
    19     @Override
    20     protected void onCreate(Bundle savedInstanceState) {
    21         super.onCreate(savedInstanceState);
    22         setContentView(R.layout.activity_intent_first);
    23 
    24         getCallPermissions(this);
    25 
    26     }
    27 
    28     public void onClick(View view){
    29         Intent intent = new Intent();
    30         intent.setAction(Intent.ACTION_VIEW);
    31         intent.setData(Uri.parse("tel:10086"));
    32         startActivity(intent);
    33     }
    34 }

    运行效果如下:

  • 相关阅读:
    安装VMware Tools和设置屏幕
    线程
    制作数据集-解析篇
    制作数据集-应用篇
    tf.train.examle函数
    输入手写数字输出识别结果——分析篇
    输入手写数字输出识别结果
    断点续训
    UC972开发板,参考实验8,完成定时器触发信号输出实验
    hz和s和脉冲
  • 原文地址:https://www.cnblogs.com/xiobai/p/10775819.html
Copyright © 2011-2022 走看看