安卓测试中,如何知道当前的activity的名字?

最简单的方法是使用adb去看,但是这个每次切换一次,就得手动一次,于是做了个自动的简单工具

原理:工具后台运行一个服务,不停的去查看当前acivity栈栈顶的activity,并将其使用logcat打印出来。

服务部分代码:

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

public class myService extends Service{
	ActivityManager activityManager=null;
	Handler handler=new Handler();
	String activity=null;
	String activity_last=null;
	Runnable runable=new Runnable()
	{

		@Override
		public void run() {
			List<RunningTaskInfo>runningTaskInfo=activityManager.getRunningTasks(1);
			activity=(runningTaskInfo.get(0).topActivity).toString();
			if(!(activity.equals(activity_last)))
			{
				System.out.println("当前activity是"+activity);
				activity_last=activity;

			}
			handler.postDelayed(runable, 10);
		}
	};
	public void onStart(Intent intent, int startId) {
		handler.postDelayed(runable, 10);
	};
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		activityManager=(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
	
		
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}



}
activity部分就两个个按钮,用来启动或者停止服务。

将这个小工具安装到测试手机上,连接上eclipse,每次手机切换activity就会把当前的activity名反馈到logcat上。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值