2014-02-22 55 views
0

這是我在主屏幕上創建快捷方式的代碼。 Everthing工作正常,但當我點擊主屏幕上的快捷方式時,它顯示「應用程序未安裝」。我想修復它。應用程序未安裝快捷方式

代碼

appPref = getSharedPreferences("isFirstTime", 0); 
isFirstTime = appPref.getBoolean("isFirstTime", true); 

if (isFirstTime) { 
    // Create explicit intent which will be used to call Our application 
    // when some one clicked on short cut 
    Intent shortcutIntent = new Intent(getApplicationContext(), 
      MainActivity.class); 
    shortcutIntent.setAction(Intent.ACTION_MAIN); 
    Intent intent = new Intent(); 

    // Create Implicit intent and assign Shortcut Application Name, Icon 
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Get Quote"); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
      Intent.ShortcutIconResource.fromContext(
        getApplicationContext(), R.drawable.ic_launcher)); 
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
    getApplicationContext().sendBroadcast(intent); 

    // Set preference to inform that we have created shortcut on 
    // Homescreen 
    SharedPreferences.Editor editor = appPref.edit(); 
    editor.putBoolean("isFirstTime", false); 
    editor.commit(); 

} 

}

回答

0

你並不需要使用的ApplicationContext,使用this(活動)將被罰款

我沒有測試這個理論,但我會猜你的問題是Intent intent = new Intent();,嘗試使用PendingIntent代替

你的短語utIntent還應該採取特定於您的應用程序(或架構類型)或您想要打開的類的操作。您提供的操作是通用的,可能會導致您的問題(設置操作會覆蓋您在創建意圖時通過的內容,這裏沒有必要)

創建快捷方式也不是個好主意用戶,它可能會惹惱他們足以將其卸載爲惡意軟件

相關問題