android adb broadcast,adb shell am broadcast -a  -d

$adb shell am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///mnt/sdcard/

作用: adb shell 发送广播mount SD卡

发送广播消息

【am start -n

{包名(package)}/{包名}.{活动activity名称}】启动某一个app的activity,配置参考AndroidManifest.xml,例如闹钟:

# am start -n

com.android.alarmclock/com.android.alarmclock.AlarmClock

【am broadcast

-a android.intent.action.Action.Name】发送广播消息

am命令用法:

usage: am

[subcommand]

[options]

start an

Activity: am start

[-D]

[-W]

<INTENT>

-D: enable

debugging

-W: wait

for launch to complete

start a

Service: am startservice

<INTENT>

send a

broadcast Intent: am

broadcast

<INTENT>

start an

Instrumentation: am

instrument [flags]

<COMPONENT>

-r: print

raw results (otherwise decode

REPORT_KEY_STREAMRESULT)

-e

<NAME>

<VALUE>:

set argument

<NAME>

to

<VALUE>

-p

<FILE>:

write profiling data to

<FILE>

-w: wait

for instrumentation to finish before

returning

start

profiling: am profile

<PROCESS>

start

<FILE>

stop

profiling: am profile

<PROCESS>

stop

start

monitoring: am monitor

[--gdb ]

--gdb: start gdbserv

on the given port at

crash/ANR

<INTENT>

specifications include these

flags:

[-a

<ACTION>]

[-d

<DATA_URI>]

[-t

<MIME_TYPE>]

[-c

<CATEGORY>

[-c

<CATEGORY>]

...]

[-e|--es

<EXTRA_KEY>

<EXTRA_STRING_VALUE>

...]

[--esn

<EXTRA_KEY>

...]

[--ez

<EXTRA_KEY>

<EXTRA_BOOLEAN_VALUE>

...]

[-e|--ei

<EXTRA_KEY>

<EXTRA_INT_VALUE>

...]

[-n

<COMPONENT>]

[-f

<FLAGS>]

[--grant-read-uri-permission]

[--grant-write-uri-permission]

[--debug-log-resolution]

[--activity-brought-to-front]

[--activity-clear-top]

[--activity-clear-when-task-reset]

[--activity-exclude-from-recents]

[--activity-launched-from-history]

[--activity-multiple-task]

[--activity-no-animation]

[--activity-no-history]

[--activity-no-user-action]

[--activity-previous-is-top]

[--activity-reorder-to-front]

[--activity-reset-task-if-needed]

[--activity-single-top]

[--receiver-registered-only]

[--receiver-replace-pending]

[<URI>]

通过命令行执行adb shell am broadcast发送广播通知。

adb shell am

broadcast 后面的参数有:

[-a ]

[-d ]

[-t ] [-c [-c ] ...] [-e|--es ...] [--ez ...] [-e|--ei ...] [-n ]

[-f ] []

例如:

adb shell am broadcast -a

com.android.test --es test_string "this is test string"--ei test_int100

--ez test_boolean true

说明:蓝色为key,红色为Value,分别为String类型,int类型,boolean类型

specifications include these flags:

[-a ] [-d ] [-t ]

2.发送broadcast,比如在模拟器中发生关机的broadcast

F:\>adb shell am broadcast -a

android.intent.action.ACTION_SHUTDOWN

-c android.intent.category.HOME

-n com.andy.androidtest/.ShutdownBroadcastReceiver

结果:

Broadcasting: Intent { act=android.intent.action.ACTION_SHUTDOWN

cat=[android.intent.category.HOME]

cmp=com.andy.androidtest/.ShutdownBroadcastReceiver }

Broadcast completed: result=0

参考:http://blog.csdn.net/infsafe/article/details/9130609

On my current project, we need to “pre-load”

many tablets with a set of videos. We want to script the loading of

the videos, so we started with adb. The videos should appear in

Android’s gallery app so they can be viewed like any other video.

Finally, we have some folder structure for the videos that should

also appear in the gallery.

Getting this working was a couple hours’ challenge. Copying the

files on to the tablet was easy. We did that in bulk using adb’s

push command:adb push

Movies/ /mnt/sdcard/Movies/remote_video.mp4

This is not enough to make the video appear in the gallery. Rather

than scanning or watching the filesystem, Android’s gallery reads a

ContentProvider that lists the media on the device. The

ContentProvider is populated by a MediaScannerService that looks

for media at particular times and saves information about any

discovered music, videos or images.

We needed a way to trigger the MediaScanner. Naively, we tried

restarting the tablet, but that didn’t get the videos in to the

gallery. Next, we asked google. We found several general

descriptions of the process, but not in enough detail to easily

duplicate it. Here is what we discovered after experimentation.

The basic form of the command is:

adb shell "am broadcast

-a -d "

or you can run it within an dab shell on a device:

adb shell

am broadcast -a -d

What should the and values be?

Action needs to be a recognized constant value from the Android

documentation. I kept tripping myself up by using the constant

name, rather than the value. Values are in the documentation for

the constants.

The two that we found useful were android.intent.action.MEDIA_MOUNTED and

android.intent.action.MEDIA_SCANNER_SCAN_FILE. As

described in their respective documentation, MEDIA_MOUNTED takes a path to scan as its

data_uri and MEDIA_SCANNER_SCAN_FILE takes a path

to a single file. For our use case, we ran the command:

adb -d shell "am broadcast -a

android.intent.action.MEDIA_MOUNTED -d

file:///mnt/sdcard/Movies/"

We had originally tried to use /mnt/sdcard/ as the full path,

because it didn’t seem that there would be any harm in scanning the

whole card. However, because different system apps watch paths on

the sdcard, trying to say the whole card was mounted causes a

permissions error.

Because of that error, we had to be specify /Movies/, which did not

trigger the error. If we do need to

load music or images, we will be able to pass those specific paths

with the same intent.

(Note: I’ve been unable to reproduce the permissions error more

recently, so there is another factor involved. One difference could

be that the tablets reporting the error were running 4.0.3, but we

have since moved to tablets running 4.1.1.)

参考:http://blog.csdn.net/loongggdroid/article/details/18985841

1 String MEDIA_MOUNTED_ACTION 广播:扩展介质(SD卡)被插入,而且已经被挂载。

2 String MEDIA_REMOVED_ACTION 广播:扩展介质被移除。

发送广播重新挂载SD卡,使图库可以及时显示自己保存的图片(无需手机重启)

安卓系统挂载sd卡后系统发出的广播是android.intent.action.MEDIA_MOUNTED,action是MEDIA_MOUNTED.

privatevoidsendBroadCaseRemountSDcard() {

Intent intent =newIntent();

// 重新挂载的动作

intent.setAction(Intent.ACTION_MEDIA_MOUNTED);

// 要重新挂载的路径

intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));

sendBroadcast(intent);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值