Android自定义抓包调用其它应用程序

啥也不说,上代码。代码懂了,啥都懂了。
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);
    	
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值