android launch初探

1. manifest:

需要加上這兩個category:

              <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.HOME" />
 
2. main activity:

package com.mylauncher;

import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;

public class D_MyLauncherActivity extends Activity implements
OnItemClickListener, OnItemLongClickListener {

private GridView mGridView;
private List<ResolveInfo> apps;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

PackageManager manager = this.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

apps = manager.queryIntentActivities(mainIntent, 0);
Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));

mGridView = (GridView) findViewById(R.id.gridView1);
mGridView.setAdapter(new MyGridViewAdapter(this, apps));

mGridView.setOnItemClickListener(this);
mGridView.setOnItemLongClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
ResolveInfo app = apps.get(position);
Intent i = new Intent(Intent.ACTION_MAIN);
i.setComponent(new ComponentName(app.activityInfo.packageName,
app.activityInfo.name));
startActivity(i);
}

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {

final ResolveInfo app = apps.get(position);
new AlertDialog.Builder(this)
.setMessage(
"Remove " + app.loadLabel(getPackageManager())
+ " from your device?")
.setPositiveButton("OK", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

Uri uri = Uri.fromParts("package",
app.activityInfo.packageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
}
}).setNegativeButton("Cancel", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

}
}).show();

return false;
}
}

3. adapter:

package com.mylauncher;

import java.util.List;

import android.content.Context;
import android.content.pm.ResolveInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyGridViewAdapter extends BaseAdapter {

private LayoutInflater mInflater;
private Context context;
private List<ResolveInfo> apps;

public MyGridViewAdapter(Context c, List<ResolveInfo> apps) {
mInflater = LayoutInflater.from(c);
this.apps = apps;
this.context = c;
}

private class ViewHolder {
private ImageView appIcon;
private TextView appName;
}

@Override
public int getCount() {
return apps.size();
}

@Override
public Object getItem(int position) {
return apps.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;

if (view == null) {
view = mInflater.inflate(R.layout.app_shortcut, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.appIcon = (ImageView) view.findViewById(R.id.imageView1);
viewHolder.appName = (TextView) view.findViewById(R.id.textView1);
view.setTag(viewHolder);
}

ViewHolder holder = (ViewHolder) view.getTag();
ResolveInfo app = apps.get(position);
holder.appIcon.setBackgroundDrawable(app.loadIcon(context.getPackageManager()));
holder.appName.setText(app.loadLabel(context.getPackageManager()));
return view;
}

}

 

摘自 fhy_2008的專欄

發佈留言