Shortcut 快捷方式 使用 - 2(轉)

Shortcut

 

[功能]

1. Shortcut 創建 有2種方法:

* "Shortcut 快捷方式 使用" 裏面提到的 通過發送包含Shortcut 信息的 Broadcast

*  本次討論的 在桌面長按空白區域 在功能選項裏面選擇"Shortcut" 然後選擇目標程序即可 如下圖:

 

 

[代碼]

1. 創建一個class:public class Shortcut1Usage extends Activity

Java代碼
  1. public   class  Shortcut1Usage  extends  Activity {  
  2.       
  3.     @Override   
  4.     public   void  onCreate(Bundle savedInstanceState) {  
  5.         super .onCreate(savedInstanceState);  
  6.         Intent addShortcut;  
  7.           
  8.         if  (getIntent().getAction()  
  9.                 .equals(Intent.ACTION_CREATE_SHORTCUT)) {  
  10.               
  11.             addShortcut = new  Intent();  
  12.             addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,   
  13.                     "119" );  
  14.               
  15.             Parcelable icon = Intent.ShortcutIconResource.fromContext(  
  16.                     this ,R.drawable.icon);  
  17.             addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,   
  18.                     icon);  
  19.               
  20.             Intent callFirePolice =   
  21.                 new  Intent(Intent.ACTION_CALL,Uri.parse( "tel://119" ));  
  22.             addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,  
  23.                     callFirePolice);  
  24.               
  25.             setResult(RESULT_OK,addShortcut);  
  26.         } else  {  
  27.             setResult(RESULT_CANCELED);  
  28.         }  
  29.         finish();  
  30.     }  
  31. }  
public class Shortcut1Usage extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent addShortcut;
        
        if (getIntent().getAction()
                .equals(Intent.ACTION_CREATE_SHORTCUT)) {
            
            addShortcut = new Intent();
            addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, 
                    "119");
            
            Parcelable icon = Intent.ShortcutIconResource.fromContext(
                    this,R.drawable.icon);
            addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
                    icon);
            
            Intent callFirePolice = 
                new Intent(Intent.ACTION_CALL,Uri.parse("tel://119"));
            addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
                    callFirePolice);
            
            setResult(RESULT_OK,addShortcut);
        } else {
            setResult(RESULT_CANCELED);
        }
        finish();
    }
}

 

 

 

2. 修改 AndroidManifest.xml,增加 Shortcut1Usage 的 Activity

Java代碼
  1. <activity android:name= ".Shortcut1Usage" >  
  2.             <intent-filter>  
  3.                 <action android:name="android.intent.action.CREATE_SHORTCUT"  />  
  4.             </intent-filter>  
  5.         </activity>  
<activity android:name=".Shortcut1Usage">
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
            </intent-filter>
        </activity>

 

 

 

這樣就可以了

 

程序中有需要 自己看哪種方法合適 就用哪一個 現附上代碼 供需要的朋友看看

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章