2014-01-24 54 views
0

我正在从我的设备上创建已安装的应用程序的快捷方式。我想要在我的应用程序中获取所有应用程序的启动器图标。但因为我不知道该代码得到它,我用这一个的平均时间(这是我正在开发当前应用程序的图标):在Android中获取安装的快捷方式的应用程序图标

intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher)); 

你可能会请帮助我从设备中获取已安装应用程序的启动器图标?

这里是我的代码安装快捷方式。

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); 
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher)); 
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");  
context.sendBroadcast(intent); 

回答

1

直到现在,应用程序无法强制自己进入主屏幕。它基本上被添加到启动程序应用程序所维护的应用程序列表中,通常主屏幕通常由用户控制。给应用程序混乱的主屏幕的能力将是一个公开的邀请滥用。

1

如果您想要设备上所有应用程序的图标并将其显示为girdview,请按照以下代码进行操作。希望有所帮助。

MainActivity.java

public class MainActivity extends Activity { 

GridView mGrid; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    loadApps(); 

    setContentView(R.layout.activity_main); 
    mGrid = (GridView) findViewById(R.id.myGrid); 
    mGrid.setAdapter(new AppsAdapter()); 
} 

private List<ResolveInfo> mApps; 

private void loadApps() { 
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 

    mApps = getPackageManager().queryIntentActivities(mainIntent, 0); 
} 

public class AppsAdapter extends BaseAdapter { 
    public AppsAdapter() { 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     CheckableLayout l; 
     ImageView i; 

     if (convertView == null) { 
      i = new ImageView(MainActivity.this); 
      i.setScaleType(ImageView.ScaleType.FIT_CENTER); 
      i.setLayoutParams(new ViewGroup.LayoutParams(50, 50)); 
      l = new CheckableLayout(MainActivity.this); 
      l.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, 
        GridView.LayoutParams.WRAP_CONTENT)); 
      l.addView(i); 
     } else { 
      l = (CheckableLayout) convertView; 
      i = (ImageView) l.getChildAt(0); 
     } 

     ResolveInfo info = mApps.get(position); 
     i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager())); 

     return l; 
    } 


    public final int getCount() { 
     return mApps.size(); 
    } 

    public final Object getItem(int position) { 
     return mApps.get(position); 
    } 

    public final long getItemId(int position) { 
     return position; 
    } 
} 

public class CheckableLayout extends FrameLayout implements Checkable { 
    private boolean mChecked; 

    public CheckableLayout(Context context) { 
     super(context); 
    } 

    public void setChecked(boolean checked) { 
     mChecked = checked; 
//   setBackgroundDrawable(checked ? 
//     getResources().getDrawable(R.drawable.blue) 
//     : null); 
      setBackground(checked ? 
        getResources().getDrawable(R.color.black) 
        : null); 


    } 

    public boolean isChecked() { 
     return mChecked; 
    } 

    public void toggle() { 
     setChecked(!mChecked); 
    } 

} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 

<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myGrid" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp" 
    android:verticalSpacing="10dp" 

    android:horizontalSpacing="10dp" 
    android:numColumns="auto_fit" 
    android:columnWidth="60dp" 
    android:stretchMode="columnWidth" 

    android:gravity="center" /> 
+0

获得误差在i.addView(i)中。它说,方法addView(ImageView)对于ImageView类型是未定义的。 – androidBoomer

+0

也在行i =(ImageView)i.getChildAt(0),它表示,方法getChildAt(int)未定义类型ImageView – androidBoomer

+0

@androidBoomer:我的坏,抱歉!我在我的答案中更新了代码。 – VikramV

相关问题