close



  1. 如果是長按應用程序啟動圖標拖到桌面的快捷方式,那麼隻要自己配置瞭國際化的可以自動轉換,求系統長按並拖動到到桌面添加快捷方式的代碼,或是源碼位置?

  2. 如果是普通代碼創建到桌面的快捷方式,雖然配置瞭國際化,但是代碼創建的快捷方式是不隨語言的改變而改變的,因此我通過監聽local信息,刪除原快捷方式,並創建新的快捷方式,但快捷方式在刪除和創建的過程均有Toast提示,查看源碼,Toast提示貌似無法屏蔽,如有大神能屏蔽,還請多多指點,多謝!

  3. 如有其它解決方案,請指點,我實在是想不出來瞭。

  4. 如何實現安裝應用的時候在桌面創建本應用的快捷方式?


  5. 監聽local創建/刪除桌面快捷方式的代碼如下



    public class MyReceiver extends BroadcastReceiver {



    @Override
    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Log.e("MyReceiver", "---------onReceive------改語言------");

    delShortcut(context);
    addShortcut(context);

    }

    private void addShortcut(Context context) {
    Intent mIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    MyApp.del = context.getString(R.string.app_name);

    // 快捷方式的名稱
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));
    shortcut.putExtra("duplicate", true); // 不允許重復創建

    // 指定當前的Activity為快捷方式啟動的對象: 如 com.everest.video.VideoPlayer
    // 註意: ComponentName的第二個參數必須加上點號(.),否則快捷方式無法啟動相應程序
    ComponentName comp = new ComponentName(context.getPackageName(), "." + "TestLanguageActivity");

    Intent mintent= new Intent(Intent.ACTION_MAIN);
    mintent.addCategory(Intent.CATEGORY_LAUNCHER);
    mintent.setComponent(comp);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,mintent);

    // 快捷方式的圖標
    ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

    context.sendBroadcast(shortcut);
    }

    private void delShortcut(Context context) {

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

    // 快捷方式的名稱
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, MyApp.del);
    // shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));
    // 指定當前的Activity為快捷方式啟動的對象: 如 com.everest.video.VideoPlayer
    // 註意: ComponentName的第二個參數必須是完整的類名(包名+類名),否則無法刪除快捷方式
    String appClass = context.getPackageName() + "." + "TestLanguageActivity";
    ComponentName comp = new ComponentName(context.getPackageName(), appClass);
    Intent mintent= new Intent(Intent.ACTION_MAIN);
    mintent.addCategory(Intent.CATEGORY_LAUNCHER);
    mintent.setComponent(comp);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mintent);
    context.sendBroadcast(shortcut);
    }


    }





  • 1

一般來說在 Android 中添加快捷方式的有以下兩種:

在launcher的應用程序列表上,長按某一應用程序圖標創建快捷方式到桌面
在桌面上長按在彈出框中選擇快捷方式->應用程序->將添加快捷方式的程序

自動創建:

通過向launcher發送Broadcast讓launcher創建快捷方式
為應用程序的組件註冊某一個符合特定條件的IntentFilter,然後可以直接在Launcher的桌面添加啟動該組件的快捷方式。

(0)

一般來說在 Android 中添加快捷方式的有以下兩種:

在launcher的應用程序列表上,長按某一應用程序圖標創建快捷方式到桌面
在桌面上長按在彈出框中選擇快捷方式->應用程序->將添加快捷方式的程序

(0)

實現長按應用創建快捷方式:

代碼:

Intent shortcutIntent = new Intent();shortcutIntent.setClassName("packageName", "className");//shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);Intent addIntent = new Intent();addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "shortcut_name");addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));//intent.putExtra("duplicate", false);addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");            context.sendBroadcast(addIntent);

然後再在AndroidManaifest.xml文件中設置:

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

代碼中註釋裡的根據需要選擇

(0)

2條評論

  • 通過以上代碼創建的快捷方式不能自動進行國際化,而如果是自己長按操作拖到桌面的是可以的,你確定這是長按應用拖到桌面的代碼嗎??? flying_vip_521 10-10 18:46
  • 沒實現自動進行國際化~ ce_la_vie 10-11 11:04

你這是幾個問題
我回答你的第四個問題吧就,希望對你有所幫助。

一般來說在 Android 中添加快捷方式的有以下兩種:

在launcher的應用程序列表上,長按某一應用程序圖標創建快捷方式到桌面
在桌面上長按在彈出框中選擇快捷方式->應用程序->將添加快捷方式的程序
那麼能不能在應用安裝時自動將應用的快捷入口添加到桌面呢? 本文給大傢分享一下相關的經驗?
桌面是由launcher來控制的,所以我們可以通過下面兩種方式來實現快捷方式的自動創建:
通過向launcher發送Broadcast讓launcher創建快捷方式
為應用程序的組件註冊某一個符合特定條件的IntentFilter,然後可以直接在Launcher的桌面添加啟動該組件的快捷方式。
第一種方式:

/**  * 添加快捷方式到桌面 要點:    * 1.給Intent指定action="com.android.launcher.INSTALL_SHORTCUT"  * 2.給定義為Intent.EXTRA_SHORTCUT_INENT的Intent設置與安裝時一致的action(必須要有)    * 3.添加權限:com.android.launcher.permission.INSTALL_SHORTCUT  */ private void addShortcutToDesktop() {     Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");     // 不允許重建     shortcut.putExtra("duplicate", false);     // 設置名字     shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,this.getString(R.string.app_name));     // 設置圖標     shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this,                     R.drawable.ic_launcher));     // 設置意圖和快捷方式關聯程序     shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(this, this.getClass()).setAction(Intent.ACTION_MAIN));     // 發送廣播     sendBroadcast(shortcut); }

當快捷方式創建成功後,launcher將通過toast的方式提示快捷方式創建成功,其中通過
shortCutIntent.putExtra("duplicate", false);設置不能重復創建,如果快捷方式已經創建則提示快捷方式已經創建
註意如果要讓上述代碼能成功運行,我們還需要設置Uses permission

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

第二種方式和第一種有些類似,不過我們不用廣播的方式讓給launcher創建,而是通過註冊IntentFilter,由於“添加快捷方式”Action是 由Launcher通過startActivity-ForResult這一方法發出的,在Activity啟動後把初始化的快捷方式 Intent返回給Launcher應用程序,設置結果值為RESULT_OK表示正常返回。
主要代碼如下:
首先在xml中設置IntentFilter

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

復制代碼創建核心代碼:

if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {Intent shortcut = new Intent(Intent.ACTION_CREATE_SHORTCUT);     // 不允許重建     shortcut.putExtra("duplicate", false);     // 設置名字     shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,             this.getString(R.string.app_name));     // 設置圖標     shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,             Intent.ShortcutIconResource.fromContext(this,                     R.drawable.ic_launcher));     // 設置意圖和快捷方式關聯的程序     shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,             new Intent(this, this.getClass()));             //將結果返回到launcher        setResult(RESULT_OK, intent);           }

在launcher中我們運行程序就可以將快捷方式創建在桌面上。

通過上述方式可以自動將快捷方式創建到桌面上,但是每次運行程序時都會將快捷方式創建到桌面上,下面我們將通過程序判斷快捷方式是否已經創建到桌面上瞭,基本思路是:由於快捷方式launcher管理的,我們可以通過查看launcher中是否已經有此快捷方式數據,如果有就不在創建。
主要代碼如下:

/**  * 添加權限:<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>  *    * @return  */ private boolean hasInstallShortcut() {     boolean hasInstall = false;     final String AUTHORITY = "com.android.launcher.settings";     Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY             + "/favorites?notify=true");     Cursor cursor = this.getContentResolver().query(CONTENT_URI,             new String[] { "title", "iconResource" }, "title=?",             new String[] { this.getString(R.string.app_name) }, null);     if (cursor != null && cursor.getCount() > 0) {         hasInstall = true;     }     return hasInstall; }

上述查詢操作,需要具有以下權限:

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

註意通過程序創建的快捷方式不會隨著程序卸載而自動刪除。

(0)



Orignal From: Android如何實現桌面快捷方式的國際化和自動創建(應用安裝時)?

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 x0z7blog01 的頭像
    x0z7blog01

    x0z7blog01的部落格

    x0z7blog01 發表在 痞客邦 留言(0) 人氣()