Saturday, November 16, 2013

android develop log–enum activity and start activity

1.check has installed app

public static Boolean hasApp(String packageName, String className) {
Boolean f = false;
try {
if ((null != className) && (className.length() > 0)) {
if (null != PkrssActivity.s_pkrss.getPackageManager().getPackageInfo(packageName, 0))
f = true;
return f;
}

try {
PackageInfo pi = PkrssActivity.s_pkrss.getPackageManager().getPackageInfo(packageName, 0);

Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);

List<ResolveInfo> apps = PkrssActivity.s_pkrss.getPackageManager().queryIntentActivities(resolveIntent, 0);

ResolveInfo ri = apps.iterator().next();
if (ri != null) {
if (ri.activityInfo.name == className)
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {

}
return f;
}



2.enum installed application list

    private static JSONArray fetch_installed_apps() {
List<ApplicationInfo> packages = PkrssActivity.s_pkrss.getPackageManager().getInstalledApplications(0);
JSONArray ret = new JSONArray();
Iterator<ApplicationInfo> l = packages.iterator();
while (l.hasNext()) {
try {
ApplicationInfo app = (ApplicationInfo) l.next();
String label = "";
label = PkrssActivity.s_pkrss.getPackageManager().getApplicationLabel(app).toString();
JSONObject obj = new JSONObject();
obj.put("name", label); // app.name
obj.put("id", app.packageName);

obj.put("backupAgentName", app.backupAgentName);
obj.put("className", app.className);
obj.put("dataDir", app.dataDir);
obj.put("descriptionRes", app.descriptionRes);
obj.put("enabled", app.enabled);
obj.put("flags", app.flags);
obj.put("icon", app.icon);
//obj.put("installLocation", app.installLocation);
obj.put("labelRes", app.labelRes);
//obj.put("logo", app.logo);
obj.put("manageSpaceActivityName", app.manageSpaceActivityName);
obj.put("metaData", app.metaData);
//obj.put("name", (app.name!=null ? app.name : ""));
//obj.put("nativeLibraryDir", app.nativeLibraryDir);
obj.put("nonLocalizedLabel", app.nonLocalizedLabel);
obj.put("packageName", app.packageName);
obj.put("permission", app.permission);
obj.put("processName", app.processName);
obj.put("publicSourceDir", app.publicSourceDir);
//obj.put("resourceDirs", app.resourceDirs());
obj.put("sharedLibraryFiles", app.sharedLibraryFiles);
obj.put("sourceDir", app.sourceDir);
obj.put("targetSdkVersion", app.targetSdkVersion);
obj.put("taskAffinity", app.taskAffinity);
obj.put("theme", app.theme);
obj.put("uid", app.uid);

ret.put(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
return ret;
}



3.enum activity in package

    private static JSONArray fetch_app_class(String packageName) {
JSONArray ret = new JSONArray();

PackageManager pManager = PkrssActivity.s_pkrss.getPackageManager();
PackageInfo packageInfo = pManager.getPackageArchiveInfo(packageName,PackageManager.GET_ACTIVITIES);
ActivityInfo[] activityInfos = packageInfo.activities;
ActivityInfo activityInfo;
try{
for(int i=0,c=activityInfos.length;i<c;++i){
activityInfo = activityInfos[i];
JSONObject obj = new JSONObject();
obj.put("packageName", activityInfo.packageName);
obj.put("name",activityInfo.name);
ret.put(obj);
}
}catch (Exception e) {
e.printStackTrace();
}
return ret;
}



4.start activity by package name and class name

String packageName = args.getString(0);
String className = args.optString(1);
// openApp(packageName,className);

Intent intent = new Intent();
if(className.isEmpty() || (className=="null")){
intent = PkrssActivity.s_pkrss.getPackageManager().getLaunchIntentForPackage(packageName);
}else{
intent = new Intent();
intent.setClassName(packageName, className);
}
if(intent != null)
PkrssActivity.s_pkrss.startActivity(intent);
return true;




 


5.start activity by action name

String actionName = args.getString(0);
Intent intent = new Intent();
intent.setAction(actionName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PkrssActivity.s_pkrss.startActivity(intent);
return true;


6.open url activity

    private static void openIntentUrl(String url) {
if ((url == null) || (url.length() == 0))
return;
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
PkrssActivity.s_pkrss.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}



7.install apk by url

    public static void installWebApk(String url) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse(url), "application/vnd.android.package-archive");
PkrssActivity.s_pkrss.startActivity(i);
}

No comments: