package androidStartApp.pack; import java.util.List; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class androidStartApp extends Activity { /** Called when the activity is first created. */ private EditText editText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btButton = (Button) findViewById(R.id.Button01); editText = (EditText) findViewById(R.id.EditText01); btButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub openApp(editText.getText().toString()); } }); } private boolean openApp(String packageName) { PackageInfo pi = null; try { pi = getPackageManager().getPackageInfo(packageName, 0); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null); resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER); resolveIntent.setPackage(pi.packageName); PackageManager pm = this.getPackageManager(); List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0); ResolveInfo ri = apps.iterator().next(); if (ri != null) { String packName = ri.activityInfo.packageName; String className = ri.activityInfo.name; Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); ComponentName cn = new ComponentName(packName, className); intent.setComponent(cn); startActivity(intent); } return true; } }
评论