Android-create shortcuts and determine whether a shortcut has been created

Source: Internet
Author: User

 

Generally, shortcuts are not automatically created after the android application is installed. Therefore, you can implement shortcuts when the application is started.

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

The attribute of the activity corresponding to the shortcut to be clicked.

<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"> </action>

</Intent-filter>

How can I determine whether a shortcut has been created? Because the shortcut information is saved in the favorites table of launcher. db of com. android. launcher, You can query this table to obtain it,

Permission required <uses-permission android: name = "com. android. launcher. permission. READ_SETTINGS"/>

The following is an example:

 

Import android. app. Activity;

Import android. content. ContentResolver;

Import android. content. Intent;

Import android. content. Intent. javascuticonresource;

Import android. database. Cursor;

Import android. graphics. BitmapFactory;

Import android.net. Uri;

Import android. OS. Bundle;

Public class AddShortCutActivity extends Activity {

/** Called when the activity is first created .*/

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

Boolean flag = IfaddShortCut (); // if it has been created, you do not need to create

If (flag = false ){

AddShortCut ();

}

}

Public void addShortCut (){

Intent shortcut = new Intent ("com. android. launcher. action. INSTALL_SHORTCUT ");

// Set attributes

Shortcut. putExtra (Intent. EXTRA_SHORTCUT_NAME, getResources (). getString (R. string. app_name ));

Export cuticonresource iconRes = Intent. Reset cuticonresource. fromContext (AddShortCutActivity. this, R. drawable. icon );

Shortcut. putExtra (Intent. EXTRA_SHORTCUT_ICON, iconRes );

// Whether repeated creation is allowed

Shortcut. putExtra ("duplicate", false );

Intent intent = new Intent (Intent. ACTION_MAIN );

Intent. setFlags (Intent. FLAG_ACTIVITY_RESET_TASK_IF_NEEDED );

Intent. addFlags (Intent. FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY );

Intent. addCategory (Intent. CATEGORY_LAUNCHER );

Intent. setClass (AddShortCutActivity. this, AddShortCutActivity. class );

// Set the Startup Program

System. out. println ("createIcon ");

Shortcut. putExtra (Intent. EXTRA_SHORTCUT_INTENT, intent );

AddShortCutActivity. this. sendBroadcast (shortcut );

}

Public boolean IfaddShortCut (){

Boolean isInstallShortcut = false;

Final ContentResolver cr = AddShortCutActivity. this. getContentResolver ();

// My 2.2 system is "com. android. launcher2.settings". For more information, see "com. android. launcher. settings"

Final String AUTHORITY = "com. android. launcher2.settings ";

Final Uri CONTENT_URI = Uri. parse ("content: //" +

AUTHORITY + "/favorites? Optional Y = true ");

Cursor c = cr. query (CONTENT_URI,

New String [] {"title", "iconResource "},

"Title =? ",

New String [] {getString (R. string. app_name)}, null); // XXX indicates the application name.

If (c! = Null & c. getCount ()> 0 ){

IsInstallShortcut = true;

System. out. println ("created ");

}

Return isInstallShortcut;

}

}

 

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"

Package = "com. shao. add"

Android: versionCode = "1"

Android: versionName = "1.0" type = "codeph" text = "/codeph">

<Uses-sdk android: minSdkVersion = "8"/>

 

<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">

<Activity android: name = ". AddShortCutActivity"

Android: label = "@ string/app_name">

<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"> </action>

</Intent-filter>

</Activity>

 

</Application>

<Uses-permission android: name = "com. android. launcher. permission. READ_SETTINGS"/>

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

</Manifest>

From: ye of a person

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.