啥也不说,上代码。代码懂了,啥都懂了。
package com.example.test321;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn1,btn2,btn3,btn4;
private static List<String> name=null;
private static String txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button) findViewById(R.id.button1);//点击则查看所有程序包名
btn2=(Button) findViewById(R.id.button2);//点击则调用酷狗
btn3=(Button) findViewById(R.id.button3);//点击则调用QQ
btn4=(Button) findViewById(R.id.button4);//点击则自定义抓包
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//name.add(txt);
Toast.makeText(MainActivity.this,getTxt(),Toast.LENGTH_SHORT).show();
if(getAllApps(MainActivity.this)){
Intent it =new Intent(MainActivity.this,Outs.class);
it.putExtra("txtname", getTxt());
startActivity(it);
}
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
callApp("com.kugou.android");
}
});
btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
callApp("com.tencent.mobileqq");
}
});
btn4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setDialog();
}
});
}
public static boolean getAllApps(Context context){
try {
//List<PackageInfo> apps = new ArrayList<PackageInfo>();
PackageManager pManager = context.getPackageManager();
List<PackageInfo> pkList = pManager.getInstalledPackages(0);
for(int i=0;i<pkList.size();i++){
PackageInfo pk = (PackageInfo)pkList.get(i);
txt+=pk.packageName+"\n";
// apps.add(pk);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public static String getTxt() {
return txt;
}
public String setDialog(){
final EditText mEditText = new EditText(this);
Dialog dia=new AlertDialog.Builder(this).setTitle("请输入调用程序的相应包名").setIcon(android.R.drawable.ic_dialog_info).setView(mEditText)
.setPositiveButton("调用",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
callApp(mEditText.getText().toString());
}
}
)
.setNegativeButton("取消", null).show();
return null;
}
public void callApp(String appName){
PackageManager pManager=MainActivity.this.getPackageManager();
try{
Intent it=new Intent(pManager.getLaunchIntentForPackage(appName));
startActivity(it);
}
catch(Exception e){
e.printStackTrace();
Toast.makeText(this, "请输入正确的包名", Toast.LENGTH_LONG).show();
}
}
}
package com.example.test321;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.Layout;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ScrollView;
import android.widget.TextView;
public class Outs extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
/**
* 注:标题。全屏。横竖屏均可以在布局中静态注册
*/
requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置无标题
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 强制为横屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
setContentView(R.layout.activity_out);
Intent it = getIntent();
String txtContent=it.getStringExtra("txtname");
TextView txt = (TextView) findViewById(R.id.txt);
//txt.setBackgroundDrawable(getWallpaper());
txt.setBackgroundColor(0x00000000);
txt.setTextColor(0xFF0000FF);
txt.setTextSize(20);
txt.setText(txtContent);
}
}