./res/layout/allap.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2" >
</GridView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2" >
</GridView>
</LinearLayout>
./src/com.tsots.allap/Activity_ALLAP.java
package com.tsots.allap;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;
public class Activity_ALLAP extends Activity {
Context context = Activity_ALLAP.this;
GridView gridView1;
PackageManager packageManager;
List<ApplicationInfo> allappInfo;
ArrayList<String> list_apPag = new ArrayList<String>();
ArrayList<String> list_apName = new ArrayList<String>();
ArrayAdapter<String> adapter;
AdapterView.OnItemClickListener mClickListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_allap);
gridView1 = (GridView) findViewById (R.id.gridView1);
packageManager =context.getPackageManager();
//取得Device中包括系統預設與手動安裝的所有AP
allappInfo = packageManager.getInstalledApplications(PackageManager.GET_ACTIVITIES);
queryAPInfo();
adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list_apName);
gridView1.setAdapter(adapter);
clickAction();
gridView1.setOnItemClickListener(mClickListener);
}
public ArrayList<String> queryAPInfo()
{
for(int i = 0 ; i <allappInfo.size() ; i ++)
{
ApplicationInfo appPI = allappInfo.get(i);//ApplicationInfo{2ad3d2e0 com.android.launcher}
String apPag = appPI.packageName; //com.android.launcher
list_apPag.add(apPag);
String apName = appPI.loadLabel(packageManager).toString();//Launcher
list_apName.add(apName);
}
return list_apName;
}
/*
* intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
* 有此行程式可將不同的activity看成好像是同一個應用程式
* 當你從主activity跳至另一個activity(此activity早己存在於系統之中)
* 當你按下"back"後它會跳回主activity
* 如果沒加入此程式當按下"back"後它則會離開主activity
*/
public void clickAction()
{
mClickListener = new AdapterView.OnItemClickListener ()
{
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Intent intent = getPackageManager().getLaunchIntentForPackage(list_apPag.get(position));
try
{
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
catch(NullPointerException e)
{
Toast.makeText(context, "NO Activity can be launch", Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
String errorInfo = e.toString();
Toast.makeText(context, errorInfo, Toast.LENGTH_LONG).show();
}
}
};
}
}
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;
public class Activity_ALLAP extends Activity {
Context context = Activity_ALLAP.this;
GridView gridView1;
PackageManager packageManager;
List<ApplicationInfo> allappInfo;
ArrayList<String> list_apPag = new ArrayList<String>();
ArrayList<String> list_apName = new ArrayList<String>();
ArrayAdapter<String> adapter;
AdapterView.OnItemClickListener mClickListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_allap);
gridView1 = (GridView) findViewById (R.id.gridView1);
packageManager =context.getPackageManager();
//取得Device中包括系統預設與手動安裝的所有AP
allappInfo = packageManager.getInstalledApplications(PackageManager.GET_ACTIVITIES);
queryAPInfo();
adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list_apName);
gridView1.setAdapter(adapter);
clickAction();
gridView1.setOnItemClickListener(mClickListener);
}
public ArrayList<String> queryAPInfo()
{
for(int i = 0 ; i <allappInfo.size() ; i ++)
{
ApplicationInfo appPI = allappInfo.get(i);//ApplicationInfo{2ad3d2e0 com.android.launcher}
String apPag = appPI.packageName; //com.android.launcher
list_apPag.add(apPag);
String apName = appPI.loadLabel(packageManager).toString();//Launcher
list_apName.add(apName);
}
return list_apName;
}
/*
* intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
* 有此行程式可將不同的activity看成好像是同一個應用程式
* 當你從主activity跳至另一個activity(此activity早己存在於系統之中)
* 當你按下"back"後它會跳回主activity
* 如果沒加入此程式當按下"back"後它則會離開主activity
*/
public void clickAction()
{
mClickListener = new AdapterView.OnItemClickListener ()
{
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Intent intent = getPackageManager().getLaunchIntentForPackage(list_apPag.get(position));
try
{
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
catch(NullPointerException e)
{
Toast.makeText(context, "NO Activity can be launch", Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
String errorInfo = e.toString();
Toast.makeText(context, errorInfo, Toast.LENGTH_LONG).show();
}
}
};
}
}
%%%%%%%%%%%%%%%%%延伸學習%%%%%%%%%%%%%%%%%%%%%%%%%
沒有留言:
張貼留言