zoukankan      html  css  js  c++  java
  • Android 快捷方式

    Android 快捷方式是桌面最基本的组件。它用于直接启动某一应用程序的某个组件。

    一般情况下,可以在Launcher的应用程序列表上,通过长按某一个应用程序的图标在左面上创建改该应用程序的快捷方式。另外,还可以通过两种方式在桌面上添加快捷方式:

       一:在应用程序中创建一个Intent,然后以Broadcast的形式通知Launcher创建一个快捷方式。

       二:为应用程序的组件注册某一个符合特定条件的IntentFilter,然后可以直接在Launcher的桌面添加启动该组件的快捷方式。

    下面以实例来讲解:

      一:在应用程序中添加快捷方式

         我们可以在看Launcher  的Androidmanifest.xml文件中Install-ShortcutReceiver的注册信息。

    <receiver
    
       android:name=".InstallShortcutReceiver"
    
       android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
      <Intent-filter>
    
      <action  android:name="android.launcher.action.INSTALL_SHORTCUT"/>
    
      </Intent-filter>
    
    </receiver>

    可以看出来系统是使用Intent的方式。

    也就是说我们使用BroadcastReceiver发送广播,首先应该程序需要有com.android.launcher.permission.INSTALL_SHORTCUT权限,完后以广播出去的Intent的action设置为com.android.launcher.action.INSTALL_SHORTCUT.这样就可以发送给Launcher的InstallShortReceiver了。

       下面以一个Demo为例,AndroidManifest.xml中注意添加:

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT">

    jing是一张ICON图标

    下面添加一个按钮,在Button的OnClickerListener中添加如下:

    String numtoDial="110";

    Intent directCall = new Intent(ACTION_CALL);

    directCall.putExtra(Intent.EXTRA_SHORTCUT_NAME,numToDial);

    directCall.setData(Uri.parse("tel://"+numToDial))

    Intent addShortcut=new Intent(ACTION_ADD_SHORT);

    Parcelable Icon=Intent.ShortcutIconResource.fromContext(this,R.drawable.jing);

    addShortcut.putEXTRA(Intent.EXTRA_SHORTCUT_INTENT,directCall);

    addShortcut.putEXTRA(Intent.EXTRA_SHORTCUT_ICON_RESOUCE,icon);

    sendBroadcast(addShortcut);

    这样就完成了。点击就可以看见效果。

    二:在Launcher添加应用程序的快捷方式。

       当我们在Launcher的桌面空白处长按触摸时,会出现一个对话框,提示选择添加的桌面组件。

      这里会出现快捷方式的选项。对话框显示出了可添加的快捷方式的Activity所属的应用程序的图标和名称的列表。当我们想把添加快捷方式的Activity添加到这一列表中时,只需要在这个Activity注册时添加一个Action为android.intent.action.CREATE_SHORTCUT的IntentFilter就可以了。

    <activity android:name=".UrgentCall">

    .....

    <intent-filter>

    <action android:name="android.intent.action.MAIN">

    <category android:name="android.intent.category.LAUNCHER">

    </intent-filter>

    <intent-filter>

    <action android:name="android.intent.action.CREATE_SHORTCUT"/>

    </intent-filter>

    .....

    </activity>

    这样,在Select Shortcut中就有了我们添加的这个Activity的添加。

    这个的原理只是在所有的Activity中查询是否有android.intent.action.CREATE_SHORTCUT的 Action而已。这样会启动对于的Activity,我们可以在这个里面直接添加代码,直接添加Intent,然后Broadcast.

    Activity {

    @Override

     public void OnCreate(Bundle saveInstantceState)

    {

        super.onCreate(saveInstantceState);

       Intent addshortcut;

     if(getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT))

    {

       addShortcut =new Intent();

       addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,"119");

       Parcelable icon=Intent.ShortcutIconResource.fromContext(this,R.drawable.jing);

       addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);

       Intent callFirePolice = new Intent(Intent.ACTION_CALL,Uri.parse("tel://119"));

       addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,callFilePolice);

       setResult(RESULT_OK,addShortcut);

    }

    else

    {

        setResult(RESULT_CANCELED);

    }

         finish();

     }

    }

    注意需要在AndroidManifest.xml文件中添加

    <intent-filter>

    <action android:name="android.Intent.action.CRATE_SHORTCUT"/>

    </intent-filter>

    }

    本文虽然简单,但是还是希望大家能够实际操作一把。

  • 相关阅读:
    mysql把查询结果集插入到表理
    js遍历json数据
    php事务回滚
    win10定时执行php脚本
    php输出json的内容
    图像的几个基本概念
    linux系统编程之I/O内核数据结构
    linux系统编程之错误处理
    深拷贝和浅拷贝
    mysql用户的创建
  • 原文地址:https://www.cnblogs.com/firecode/p/2854519.html
Copyright © 2011-2022 走看看