Android中程式向案頭和Launcher添加捷徑【安卓進化三十二】

來源:互聯網
上載者:User

        最近感覺這個添加捷徑挺有趣的,就查資料自己寫了個demo---簡單的例子,這個例子就是有兩個按鈕,點擊“將此程式添加到捷徑”,則手機案頭增加一個捷徑,同時launcher中也多了一個捷徑,點擊退出,則提示:toast彈提示資訊“退出程式”。知識梳理:Android平台上添加捷徑有兩種:一種案頭的捷徑,一種是launcher的捷徑。原理:是通過intent封裝一些資訊,以Broadcast的形式通知launcher建立捷徑的!一定不要忘記在manifest.xml中註冊一下許可權:

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

在manifest.xml中加入一個動作過濾的intentFilter,捷徑的列表中會多個該程式的捷徑。

有問題或向說點什麼的可以留言,歡迎大家批評和指正,轉載請標明出處:

下面看一下程式的:  

                      
      程式的開始介面:                                     點擊“將此程式添加捷徑”按鈕:

                                                     

         點擊退出按鈕,案頭多了捷徑,彈Toast:            點出選擇捷徑後多了程式的捷徑:

                                                       

在IntentWidget工程中:

一、在com.cn.daming包中IntentWidgetMainActivity.java中的代碼:

 package com.cn.daming;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;import android.widget.Toast;public class IntentWidgetMainActivity extends Activity implements OnClickListener{private Button mStartWidgetButton;private Button mExitButton;@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mStartWidgetButton = (Button) findViewById(R.id.my_button_1);        mExitButton = (Button) findViewById(R.id.my_button_2);        mStartWidgetButton.setOnClickListener(this);        mExitButton.setOnClickListener(this);    }public void onClick(View v){if(v == mStartWidgetButton){//inint the widgetIntent is declearIntent addWidgetIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");                     //whether repeat create is or not                      addWdgetIntent.putExtra("duplicate",true);//set the Widget of the titleString mTitle = getResources().getString(R.string.my_title);//set the Widget of the iconParcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.widget_image);Intent mIntent = new Intent(this,IntentWidgetMainActivity.class);addWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, mTitle);//set the titleaddWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);//set the iconaddWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mIntent);//set the intentsendBroadcast(addWidgetIntent);}else if(v == mExitButton){finish();Toast.makeText(IntentWidgetMainActivity.this, R.string.exit, Toast.LENGTH_SHORT).show();}}}

二、在layout目錄下的main.xml中的代碼:

<?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"    android:background="#00ffffff"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_marginTop="15dip"    android:layout_marginBottom="15dip"    android:gravity="center"    android:text="@string/hello"    android:textSize="8pt"    /><Button    android:id="@+id/my_button_1"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_marginBottom="10dip"    android:textSize="10pt"    android:text="@string/my_button_1"/><Button    android:id="@+id/my_button_2"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_marginBottom="10dip"    android:textSize="10pt"    android:text="@string/my_button_2"/><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_marginBottom="15dip"    android:gravity="center"    android:text="@string/blogs"    android:textSize="8pt"    /></LinearLayout>

三、在values下的string.xml中的代碼:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">這是大明添加到Launcher的捷徑</string>    <string name="app_name">大明捷徑!</string>    <string name="my_button_1">將此程式添加捷徑</string>    <string name="my_button_2">退出程式</string>    <string name="my_title">大明程式</string>    <string name="exit">程式正在退出。。。。。。</string>    <string name="blogs">部落格地址:\n http://blog.csdn.net/wdaming1986/article/details/6877154</string></resources>

 

四、manifest.xml 中的代碼

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.cn.daming"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".IntentWidgetMainActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>            <!-- add the launch of my programmer`s quick launcher-->            <intent-filter>                <action android:name="android.intent.action.CREATE_SHORTCUT"/>            </intent-filter>        </activity>    </application>    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/></manifest>

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.