2014-01-24 55 views
6

我目前正在开发一个应用程序,可以创建设备的所有安装应用程序的快捷方式。为了创建快捷方式,用户需要以管理员身份登录,并从该活动登录,其中包含一个ListView,其中包含所有已安装的应用程序。用户需要选择app/s,以便创建快捷方式。如何在Android的自定义启动器中创建应用快捷方式?

private boolean Generate_Shortcut(Context context, String appName, boolean isTrue) { 
    boolean flag =false ; 
    int app_id=-1; 
    PackageManager pm = context.getPackageManager(); 
    Intent i = new Intent(Intent.ACTION_MAIN); 
    i.addCategory(Intent.CATEGORY_LAUNCHER); 
    List<ResolveInfo> res = pm.queryIntentActivities(i,0); 


for(int k=0; k<res.size(); k++) { 
    if(res.get(k).activityInfo.loadLabel(pm).toString().equals(appName)){ 
     flag = true; 
     app_id = k; 

     break; 
    } 
} 

if(flag) { 
    ActivityInfo ai = res.get(app_id).activityInfo; 

    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); 
    shortcutIntent.setClassName(ai.packageName, ai.name); 
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    Intent intent = new Intent(); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName); 
    intent.putExtra("duplicate", false); 

    if(isTrue) {  
     // CREATE SHORTCUT 
     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher)); 
     intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");  
    } else { 
     // REMOVE SHORTCUT 
     intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); 
    } 

    context.sendBroadcast(intent); 

} else 
    System.out.println("applicaton not found"); 
return true; 
} 

选择应用程序后,它将在主活动中创建一个具有GridView的快捷方式。这里的问题是创建的快捷方式是在主屏幕中创建的。现在我的问题是,如何在我的应用程序(MainActivity.java)中创建一个快捷方式,而不是在主屏幕中?

+0

请您详细说明“在当前正在开发的应用程序中创建一个快捷方式”吗?你想创造一种发射器吗? –

+0

@AndrewT。,是的,这就是我想要做的。我想在我的应用程序内创建一个启动器。 – androidBoomer

+0

由于快捷方式位于您自己的应用程序内,因此您可以根据自己的需要进行操作。只需存储您已经显示给用户的信息,然后使用它。 –

回答

3

可以在您的应用程序中显示将启动另一个应用程序的图标。您可以使用PackageManager发现已安装的应用程序并获取您应该显示的启动器图标。

  1. 从应用程序的上下文中获取PackageManager

    PackageManager pm = context.getPackageManager(); 
    
  2. 创建将用于查询存在的安装的应用程序,并会显示在启动的意图。

    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
    
  3. 查询的对于其匹配的IntentFilter我们mainIntent任何应用程序。

    List<ResolveInfo> apps = pm.queryIntentActivities(mainIntent, 0); 
    
  4. 迭代返回给获得必要的创建启动应用程序,并显示为启动器图标资源的意图的细节ResolveInfo名单。

    for (ResolveInfo app : apps) { 
        // Create an Intent that can be use to launch the application 
        Intent intent = new Intent(Intent.ACTION_MAIN); 
        intent.addCategory(Intent.CATEGORY_LAUNCHER); 
        intent.setComponent(new ComponentName(app.activityInfo.applicationInfo.packageName, app.activityInfo.name)); 
    
        // Get the icon to display in your UI 
        Drawable icon = app.activityInfo.loadIcon(pm); 
    } 
    

UPDATE: 您可以在这里找到创建自定义启动的walkthru:http://arnab.ch/blog/2013/08/how-to-write-custom-launcher-app-in-android/

+0

这是我的完整代码。见上面的编辑。 – androidBoomer

+0

顺便说一下,我应该在哪里使用答案最后部分的“可绘制图标”? – androidBoomer

+0

您需要使用可绘制图标,但是您计划在应用内显示快捷方式。你需要实现UI。对于自定义启动器,我过去曾使用[GridView](http://developer.android.com/guide/topics/ui/layout/gridview.html)。 – csmurphy84

4

你试试?

<activity android:name=".YourActivity" android:label="@string/app_name"> 
    <intent-filter> 
    <action android:name="android.intent.action.CREATE_SHORTCUT" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 

然后在接收意图的活动中,您为您的快捷方式创建一个意图并将其作为活动结果返回。

// create shortcut if requested 
ShortcutIconResource icon = 
    Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); 

Intent intent = new Intent(); 

Intent launchIntent = new Intent(this,ActivityToLaunch.class); 

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent); 
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname()); 
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 

setResult(RESULT_OK, intent); 
+1

它不工作... – androidBoomer

相关问题