|
Android example source code file (ActivityPickerActivity.java)
The ActivityPickerActivity.java Android example source code
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.anycut;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.google.android.photostream.UserTask;
/**
* Presents a list of activities to choose from. This list only contains activities
* that have ACTION_MAIN, since other types may require data as input.
*/
public class ActivityPickerActivity extends ListActivity {
PackageManager mPackageManager;
/**
* This class is used to wrap ResolveInfo so that it can be filtered using
* ArrayAdapter's built int filtering logic, which depends on toString().
*/
private final class ResolveInfoWrapper {
private ResolveInfo mInfo;
public ResolveInfoWrapper(ResolveInfo info) {
mInfo = info;
}
@Override
public String toString() {
return mInfo.loadLabel(mPackageManager).toString();
}
public ResolveInfo getInfo() {
return mInfo;
}
}
private class ActivityAdapter extends ArrayAdapter<ResolveInfoWrapper> {
LayoutInflater mInflater;
public ActivityAdapter(Activity activity, ArrayList<ResolveInfoWrapper> activities) {
super(activity, 0, activities);
mInflater = activity.getLayoutInflater();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ResolveInfoWrapper info = getItem(position);
View view = convertView;
if (view == null) {
// Inflate the view and cache the pointer to the text view
view = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
view.setTag(view.findViewById(android.R.id.text1));
}
final TextView textView = (TextView) view.getTag();
textView.setText(info.getInfo().loadLabel(mPackageManager));
return view;
}
}
private final class LoadingTask extends UserTask<Object, Object, ActivityAdapter> {
@Override
public void onPreExecute() {
setProgressBarIndeterminateVisibility(true);
}
@Override
public ActivityAdapter doInBackground(Object... params) {
// Load the activities
Intent queryIntent = new Intent(Intent.ACTION_MAIN);
List<ResolveInfo> list = mPackageManager.queryIntentActivities(queryIntent, 0);
// Sort the list
Collections.sort(list, new ResolveInfo.DisplayNameComparator(mPackageManager));
// Make the wrappers
ArrayList<ResolveInfoWrapper> activities = new ArrayList
Other Android examples (source code examples)Here is a short list of links related to this Android ActivityPickerActivity.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.