Android 發送多個不同的快捷方式(shortcut)到桌面並向其啟動的Activity傳參


需求:

對於創建快捷方式到桌面,網上能查到不少資料,但一般都是針對應用程序本身的。

前陣子在做項目時,遇到了一個類似於百度貼吧里面的一個需求:對於每個具體的貼吧,都可以將其發送到桌面(HomeScreen)建立快捷方式shortcut。

圖標相同,只是圖標下面顯示的名稱為具體貼吧的名稱,然后點擊此快捷圖標則能直接進入到本貼吧中。


實現:

1.AndroidManifest中聲明權限:

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

2.app_start.xml布局文件:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2  xmlns:tools="http://schemas.android.com/tools"
 3  android:layout_width="match_parent"
 4  android:layout_height="match_parent"
 5  android:gravity="center_horizontal"
 6  android:orientation="vertical"
 7  android:paddingBottom="20dp"
 8  android:paddingTop="20dp" >
 9 
10     <TextView 11         android:layout_width="wrap_content"
12  android:layout_height="wrap_content"
13  android:text="這是主activity" />
14 
15     <Button 16         android:id="@+id/button1"
17  android:layout_width="200dp"
18  android:layout_height="wrap_content"
19  android:layout_marginTop="30dp"
20  android:text="進入第一個貼吧" >
21     </Button>
22 
23     <Button 24         android:id="@+id/button2"
25  android:layout_width="200dp"
26  android:layout_height="wrap_content"
27  android:layout_marginTop="15dp"
28  android:text="進入第二個貼吧" >
29     </Button>
30 
31 </LinearLayout>

相應的邏輯代碼為:

 1 package com.qqyumidi.shortcutdemo;  2 
 3 import android.app.Activity;  4 import android.content.Intent;  5 import android.os.Bundle;  6 import android.view.View;  7 import android.view.View.OnClickListener;  8 import android.widget.Button;  9 
10 public class AppStart extends Activity implements OnClickListener { 11 
12     private Button button1; 13     private Button button2; 14 
15  @Override 16     protected void onCreate(Bundle savedInstanceState) { 17         super.onCreate(savedInstanceState); 18  setContentView(R.layout.app_start); 19 
20         button1 = (Button) findViewById(R.id.button1); 21         button2 = (Button) findViewById(R.id.button2); 22 
23         button1.setOnClickListener(this); 24         button2.setOnClickListener(this); 25 
26         Intent intent = getIntent(); 27         if (intent != null) { 28             String tName = intent.getStringExtra("tName"); 29             if (tName != null) { 30                 Intent redirectIntent = new Intent(); 31                 redirectIntent.setClass(AppStart.this, TiebaActivity.class); 32                 redirectIntent.putExtra("tName", tName); 33  startActivity(redirectIntent); 34  } 35  } 36  } 37 
38  @Override 39     public void onClick(View v) { 40         // TODO Auto-generated method stub
41         Intent intent = new Intent(); 42         intent.setClass(AppStart.this, TiebaActivity.class); 43         //傳參
44         switch (v.getId()) { 45         case R.id.button1: 46             intent.putExtra("tName", "玉米吧"); 47             break; 48         case R.id.button2: 49             intent.putExtra("tName", "土豆吧"); 50             break; 51  } 52  startActivity(intent); 53  } 54 }


貼吧頁tieba_activity.xml布局文件為:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2  xmlns:tools="http://schemas.android.com/tools"
 3  android:layout_width="match_parent"
 4  android:layout_height="match_parent"
 5  android:gravity="center_horizontal"
 6  android:orientation="vertical"
 7  android:paddingBottom="20dp"
 8  android:paddingTop="20dp" >
 9 
10     <TextView 11         android:id="@+id/textview1"
12  android:layout_width="wrap_content"
13  android:layout_height="wrap_content" />
14 
15     <Button 16         android:id="@+id/button3"
17  android:layout_width="200dp"
18  android:layout_height="wrap_content"
19  android:layout_marginTop="30dp"
20  android:text="生成快捷方式到桌面" >
21     </Button>
22 
23 </LinearLayout>

相應的邏輯代碼為:

package com.qqyumidi.shortcutdemo; import android.app.Activity; import android.content.Intent; import android.content.Intent.ShortcutIconResource; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class TiebaActivity extends Activity { private TextView textView; private Button button; private String tName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tieba_activity); Intent intent = getIntent(); tName = intent.getStringExtra("tName"); textView = (TextView) findViewById(R.id.textview1); textView.setText("本貼吧名稱: " + tName); button = (Button) findViewById(R.id.button3); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub
                if (tName == null) { tName = getString(R.string.app_name); } addShortCut(tName); } }); } private void addShortCut(String tName) { // 安裝的Intent 
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); // 快捷名稱 
 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, tName); // 快捷圖標是允許重復
        shortcut.putExtra("duplicate", false); Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); shortcutIntent.putExtra("tName", tName); shortcutIntent.setClassName("com.qqyumidi.shortcutdemo", "com.qqyumidi.shortcutdemo.AppStart"); shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); // 快捷圖標 
        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); // 發送廣播 
 sendBroadcast(shortcut); } }


在如上代碼中,最主要的部分為addShortCut()函數和AppStart對傳入參數的相應處理過程。

ShortCut Demo下載地址:點擊下載

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM