android adb 启动activity、service,发送broadcast等 *** 作

android adb 启动activity、service,发送broadcast等 *** 作,第1张

android adb 启动activity、service,发送broadcast等 *** 作

一、adb启动activity:

adb shellam start -n {包(package)名}/{包名}.{活动(activity)名称}

如:启动浏览器

adb shell am start -n com.android.browser/com.android.browser.BrowserActivity

二、adb关闭activity:

adb shellam force-stop {包(package)名}

如:关闭浏览器adb shellam force-stopcom.android.browser

三、adb启动service:

adb shellam startservice -n{包(package)名}/{包名}.{服务(service)名称}

如:启动自己应用中一个service

adb shellam startservice -n com.android.traffic/com.android.traffic.maniservice

四、adb卸载应用程序:

adb uninstall{包(package)名}

如:卸载浏览器adbuninstallcom.android.browser

五、adb发送broadcast:

adb shellam broadcast -a <广播动作>

如:发送一个网络变化的广播

adb shellam broadcast -a android.net.conn.CONNECTIVITY_CHANGE

六、adb端口转发:

adb shell am broadcast -a NotifyServiceStop

adb forward tcp:12580 tcp:10086

adb shell am broadcast -a NotifyServiceStart

在本地调试app中的一个service时,使用adb shell am startserviced出报错

错误信息如下:app is in background uid null,如图:

查了一些帖子发现提供的2018年左右的解决方案为:

Settings->Build,Execution,Deployment->Instant Run

关掉 Enable Instant Run to hot swap code/ 

尝试失效。

后续找到解决方案:

adb shell am start-foreground-service -n com.demo.screenrecorder/com.demo.screenrecorder.RecordService

使用这个start-foreground-service来替换startservice可以解决这个问题。特此记录。

PS:Android O 推出出了Background Execution Limits,减少后台应用内存使用及耗电,一个很明显的应用就是不准后台应用通过startService启动服务。

解决方案就是及时调用startForeground,对于O以后的还要注意Notification需要一个ChannelID

@Override

public void onCreate() {

super.onCreate()

    try {

String CHANNEL_ONE_ID ="com.demo.screenrecorder"

        String CHANNEL_ONE_NAME ="Channel One"

        NotificationChannel notificationChannel =null

        notificationChannel =new NotificationChannel(CHANNEL_ONE_ID,

                CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_DEFAULT)

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE)

        assert manager !=null

        manager.createNotificationChannel(notificationChannel)

        startForeground(1, new NotificationCompat.Builder(this, CHANNEL_ONE_ID).build())

    }catch (Exception e) {

Log.e(TAG, e.getMessage())

    }

initView()

}

推荐阅读: Android O 后台startService限制简析

android7.0启动service有两种方法:

1、 Context.startService()

调用者与服务之间没有关联,即使调用者退出,服务仍可运行

2、 Context.bindService()

调用者与服务绑定在一起,调用者一旦退出,服务也就终止


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9261750.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存