6

我是使用Robotium framework来测试应用程序的黑盒。 每次安装新应用程序时,被测应用程序都会在状态栏中发送通知。 我想点击该通知,但我仍然没有找到正确的方法来做到这一点。以编程方式点击Android通知

当我手动点击一个通知我得到这个logcat线:

I/ActivityManager( 148): START {flg=0x14000000 cmp=com.test.package/.activity.FrontActivity bnds=[0,38][240,86] (has extras) u=0} from pid -1 
I/ActivityManager( 148): START {flg=0x14000000 cmp=com.test.package/.activity.ResultActivity u=0} from pid 8600 
I/ActivityManager( 148): Displayed com.test.package/.activity.FrontActivity: +1s183ms 
I/ActivityManager( 148): Displayed com.test.package/.activity.ResultActivity: +744ms 

我知道Robotium不能在同一时间测试2个不同的应用程序,以及我知道你不能得到的通知外部应用程序。

我也试图让悬而未决的意图,并通过使用下面的代码火起来:

Context context = this.getInstrumentation().getTargetContext().getApplicationContext(); 
String intentClassString = "com.test.package.activity.FrontActivity"; 
Class<?> intentClass = null; 

try { 
    intentClass = Class.forName(intentClassString); 
} catch (ClassNotFoundException e) { 
    e.printStackTrace(); 
} 

Intent intent = new Intent(context, intentClass); 
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_NO_CREATE); 

try { 
    pendingIntent.send(); 
} catch (CanceledException e) { 
    e.printStackTrace(); 
} 

活动实际上是显示,但它是从我通过手动调用它获得不同的。我认为在我通过的情况下或者我称之为未决意图的方式有问题。

有关此问题的任何提示?有没有更好的方法来模拟点击通知?

回答

1

您可以触发通知栏上的滑动并点按通知上的事件。通过改变参数,你可以使用这些命令

adb shell input swipe 100 500 400 100 1000 

adb shell input tap 400 400 

参考命令。

0

答案Ranjith KP给你包含解决方案。请注意,您需要运行该应用程序的电话,因为这些命令与sudo权限一起使用。

这是为我工作的代码。它首先扩展通知栏(刷卡命令),等待1秒,最后点击列表上的第一个通知。根据需要修改轴参数。

Process su = null; 
try { 
su = Runtime.getRuntime().exec("su"); 
su.getOutputStream().write("input swipe 270 010 270 900\n".getBytes()); 
Thread.sleep(1000); 
su.getOutputStream().write("input tap 200 200\n".getBytes()); 
su.getOutputStream().write("exit\n".getBytes()); 
su.waitFor(); 
} catch (Exception e) { 
e.printStackTrace(); 
} finally { 
if (su != null) { 
    su.destroy(); 
} 
}