Home>

The android shortcut is the most basic component of the desktop.It is used to directly launch a component of an application.

In general,Available on the launcher's application list,Create a shortcut to change an application on the left by long pressing the icon of an application.In addition, there are two ways to add shortcuts to your desktop:

One:Create an intent in the application, and then notify the launcher to create a shortcut in the form of broadcast.

Two:Register an intent filter that meets specific conditions for the component of the application, and then you can add a shortcut to launch the component directly on the launcher's desktop.

The following simulation adds a shortcut to the application

main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <button android:id="@ + id/createshortcut"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:textsize="20px"
  android:text="Create Shortcut" />
 <button android:id="@ + id/exit"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:textsize="20px"
  android:text="Exit" />
</linearlayout>

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.ljq.action" android:versioncode="1"
 android:versionname="1.0">
 <application android:icon="@ drawable/icon"
  android:label="@ string/app_name">
  <activity android:name=". Shortcutaction"
   android:label="@ string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.main" />
    <category
     android:name="android.intent.category.launcher" />
   </intent-filter>
  </activity>
 </application>
 <uses-sdk android:minsdkversion="7" />
 <!-Add Shortcut Permission->
 <uses-permission
  android:name="com.android.launcher.permission.install_shortcut" />
</manifest>

shortcutaction class:

package com.ljq.action;
import android.app.activity;
import android.content.intent;
import android.os.bundle;
import android.os.parcelable;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
/**
 * Create shortcuts through the app
 *
 * @author jiqinlin
 *
 * /
public class shortcutaction extends activity implements onclicklistener {
 private button createshortcut=null;//Create a shortcut button
 private button exit=null;//Quit the system
 @override
 public void oncreate (bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  setcontentview (r.layout.main);
  createshortcut=(button) findviewbyid (r.id.createshortcut);
  exit=(button) findviewbyid (r.id.exit);
  createshortcut.setonclicklistener (this);
  exit.setonclicklistener (this);
 }
 public void onclick (view v) {
  //button btn=(button) v;
  switch (v.getid ()) {
  case r.id.createshortcut:
   //string title=getresources (). getstring (r.string.title);
   intent addintent=new intent ("com.android.launcher.action.install_shortcut");
   parcelable icon=intent.shortcuticonresource.fromcontext (this, r.drawable.png);//Get the shortcut icon
   intent myintent=new intent (this, shortcutaction.class);
   addintent.putextra (intent.extra_shortcut_name, "shortcut");//shortcut title
   addintent.putextra (intent.extra_shortcut_icon_resource, icon);//shortcut icon
   addintent.putextra (intent.extra_shortcut_intent, myintent);//shortcut action
   sendbroadcast (addintent);//Send broadcast
   break;
  case r.id.exit:
   system.exit (0);
   break;
  }
 }
}

operation result:

  • Previous 2 ways to bulk import data in SQL Server database
  • Next jQuery method to get the value of checkboxlist