【转】Usage of sendBroadcast()

原文网址:http://stackoverflow.com/questions/4883079/usage-of-sendbroadcast

sendBroadcast() - Should it be called inside Activity? I am trying to call sendBroadcast() from my method of utility-class which doesn't extend Activity. I am getting compilation error as below

The method sendBroadcast(Intent) is undefined for the type MyWrapperClass MyWrapperClass.java

Here is the code snippet:

abstract class MyWrapperClass {

    public static void sendData()
         {
             Intent intent = new Intent ("com.proj.utility.mgr",null);

             intent.putExtra("example","Broadcasting "); 

            sendBroadcast(intent);

         }
    }

Is there any concept behind using sendBroadcast call inside my class. There is no issue in using sendBroadcast() inside Activity. Can someone here help me to resolve it? Or Any other suggestions are invited to return data from utility class to application asynchronously. Thanks in advance.

 
2 
you need access to the application context to call sendbroadcast. Could you not get this information from android documentnation. –  doc_180 Feb 3 '11 at 6:55

2 Answers

You should pass the context from activity class to utility class to access the specific application resources like startActivity, sendBroadcast, etc.

context.sendBroadcast(intent);

转载于:https://www.cnblogs.com/wi100sh/p/4419900.html

08-29 08:44:38.869 1663 1781 I GoLog : [2025-08-29 08:44:38.868] [muse-local-server] [INFO] /util.go[313] - file /storage/emulated/0/MuseLocalServer/__common/db/download/muse.incr.250515.gz remove OK! 08-29 08:44:39.396 914 914 D AsrRecogService: stop, needDestroy false 08-29 08:44:39.425 914 914 D MsgController: clean by name:com.peasun.aispeech.sharjeck.AsrRecogService 08-29 08:44:39.586 230 4587 D NuPlayerDriver: notifyListener_l(0xacd2ae40), (2, 0, 0), loop setting(0, 0) 08-29 08:44:39.608 476 879 W Binder : Binder call failed. 08-29 08:44:39.608 476 879 W Binder : java.lang.SecurityException: Neither user 1041 nor current process has android.permission.WAKE_LOCK. 08-29 08:44:39.608 476 879 W Binder : at android.app.ContextImpl.enforce(ContextImpl.java:1585) 08-29 08:44:39.608 476 879 W Binder : at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:1617) 08-29 08:44:39.608 476 879 W Binder : at com.android.server.power.PowerManagerService$BinderService.updateWakeLockWorkSource(PowerManagerService.java:3501) 08-29 08:44:39.608 476 879 W Binder : at com.android.server.power.PowerManagerService$BinderService.updateWakeLockUids(PowerManagerService.java:3492) 08-29 08:44:39.608 476 879 W Binder : at android.os.IPowerManager$Stub.onTransact(IPowerManager.java:105) 08-29 08:44:39.608 476 879 W Binder : at android.os.Binder.execTransact(Binder.java:565) 08-29 08:44:39.615 476 879 W AppOps : Finishing op nesting under-run: uid 10040 pkg com.peasun.aispeech code 24 time=0 duration=0 nesting=0 08-29 08:44:39.630 914 914 D AIMonitorService: get asr status:asr.exit 08-29 08:44:39.634 914 914 D AirInputMethod: get asr status:asr.exit 08-29 08:44:39.636 230 721 D NuPlayerDriver: reset(0xacd2ae40) at state 6 08-29 08:44:39.636 230 721 D NuPlayerDriver: notifyListener_l(0xacd2ae40), (8, 0, 0), loop setting(0, 0) 08-29 08:44:39.644 230 4587 W AMessage: failed to post message as target looper for handler 0 is gone. 08-29 08:44:39.645 230 4587 D NuPlayerDriver: notifyResetComplete(0xacd2ae40) 08-29 08:44:39.648 230 764 D NuPlayerDriver: reset(0xacd2ae40) at state 0 08-29 08:44:39.727 961 961 E FS : 充电状态 level:2 08-29 08:44:40.277 914 914 D AIOpenUtils: broadcast,asr.exit 08-29 08:44:40.729 476 552 W ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:877 com.android.server.input.InputManagerService.interceptKeyBeforeQueueing:1890 <bottom of call stack> <bottom of call stack> <bottom of call stack> 08-29 08:44:40.739 914 914 D AIMonitorService: got keyevent:213, 0 08-29 08:44:40.740 914 914 D MicController: stopRecorder---- 08-29 08:44:40.740 914 914 I DefaultMicInputStream: close 08-29 08:44:40.740 914 914 I DefaultMicInputStream: hold recorder. 08-29 08:44:40.754 914 914 D SpeechUtils: WeatherService not running, ignore 08-29 08:44:40.755 914 914 D SpeechUtils: InteractService not running, ignore 08-29 08:44:40.757 914 914 D SpeechUtils: StockService not running, ignore 08-29 08:44:40.758 914 914 D AsrRecogService: onStartCommand=========== 08-29 08:44:40.758 914 914 D AsrRecogService: receive action msg:asr.start 08-29 08:44:40.758 914 914 D MicController: stopRecorder---- 08-29 08:44:40.758 914 914 I DefaultMicInputStream: close 08-29 08:44:40.758 914 914 I DefaultMicInputStream: hold recorder. 08-29 08:44:40.758 914 914 D AsrRecogService: Language:1536, input:asr.voice.input.default,WakeUp:0 08-29 08:44:40.762 914 1502 I DefaultMicInputStream: reset! 08-29 08:44:40.764 914 914 W AudioManager: setStreamMute is deprecated. adjustStreamVolume should be used instead. 08-29 08:44:40.765 597 768 I vol.Events: writeEvent mute_changed STREAM_MUSIC true 08-29 08:44:40.792 914 914 D AsrRecogService: is mute mode: 0 08-29 08:44:40.792 914 914 D AsrRecogService: createAsrRecogFloatView#### 08-29 08:44:40.869 914 914 D AsrRecogService: acttach view to windows========== 08-29 08:44:40.906 914 1565 D mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000 08-29 08:44:40.921 476 908 W InputMethodManagerService: Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@4caa748 (uid=10044 pid=1605) 08-29 08:44:41.046 961 961 E FS : 充电状态 level:2 08-29 08:44:41.058 961 961 E FS : KeyReceiver keycodeDown:213 action:0 08-29 08:44:41.076 961 961 E FS : KeyReceiver keycodeDown:213 action:0 08-29 08:44:41.121 914 914 D MsgController: name:com.peasun.aispeech.sharjeck.AsrRecogService 08-29 08:44:41.250 914 914 W IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection 08-29 08:44:42.257 961 961 E FS : 充电状态 level:2 08-29 08:44:42.879 1068 1068 D MyHandler: Message: 301 08-29 08:44:43.274 914 932 I AidlController: getCustomerId 08-29 08:44:43.275 1068 1068 D TVManager: got customerId 080142 08-29 08:44:43.499 961 961 E FS : 充电状态 level:2 08-29 08:44:43.598 476 551 I InputDispatcher: Window 'Window{33a478f u0 com.peasun.aispeech}' spent 2318.0ms processing the last input event: KeyEvent(deviceId=2, source=0x00000301, action=0, flags=0x00000008, keyCode=213, scanCode=94, metaState=0x00000000, repeatCount=2), policyFlags=0x42000001 08-29 08:44:43.599 914 914 D AIMonitorService: got keyevent:213, 0 08-29 08:44:43.599 914 914 D MicController: stopRecorder---- 08-29 08:44:43.599 914 914 I DefaultMicInputStream: close 08-29 08:44:43.599 914 914 I DefaultMicInputStream: hold recorder. 08-29 08:44:43.603 914 914 D SpeechUtils: WeatherService not running, ignore 08-29 08:44:43.604 914 914 D SpeechUtils: InteractService not running, ignore 08-29 08:44:43.608 914 914 D SpeechUtils: StockService not running, ignore 08-29 08:44:43.619 914 914 D AsrRecogService: onStartCommand=========== 08-29 08:44:43.620 914 914 D AsrRecogService: receive action msg:asr.start 08-29 08:44:43.620 914 914 D MicController: stopRecorder---- 08-29 08:44:43.620 914 914 I DefaultMicInputStream: close 08-29 08:44:43.620 914 914 I DefaultMicInputStream: hold recorder. 08-29 08:44:43.654 1068 1088 I Utils : top running app is : com.huiaichang.sdm.desktop/android.app.usage.UsageStats 08-29 08:44:43.654 1068 1088 D TVManager: IRemoteService, getTopPackageName:com.huiaichang.sdm.desktop 08-29 08:44:43.654 914 4618 I AidlController: get top activity:com.huiaichang.sdm.desktop 08-29 08:44:43.656 914 4618 I SceneCtrl: scene com.huiaichang.sdm.desktop 08-29 08:44:43.816 476 552 W ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:877 com.android.server.input.InputManagerService.interceptKeyBeforeQueueing:1890 <bottom of call stack> <bottom of call stack> <bottom of call stack> 08-29 08:44:43.819 961 961 E FS : KeyReceiver keycodeDown:213 action:1 08-29 08:44:43.851 961 961 E FS : KeyReceiver keycodeDown:213 action:1 08-29 08:44:44.483 914 914 D AIMonitorService: got keyevent:213, 1 08-29 08:44:44.485 914 914 D AsrRecogService: onStartCommand=========== 08-29 08:44:44.485 914 914 D AsrRecogService: receive action msg:asr.stop 08-29 08:44:44.656 914 1502 D AsrRecogService: voice less than 600ms, ignore! 08-29 08:44:44.656 914 1502 D AsrRecogService: restore volume, mute state:3, 0 08-29 08:44:44.656 914 1502 W AudioManager: setStreamMute is deprecated. adjustStreamVolume should be used instead. 08-29 08:44:44.660 914 914 D AIMonitorService: get asr status:asr.begin 08-29 08:44:44.661 597 768 I vol.Events: writeEvent mute_changed STREAM_MUSIC false 08-29 08:44:44.678 1605 1605 D IJKMEDIA: IjkMediaPlayer_setVolume 08-29 08:44:44.678 1605 1605 D IJKMEDIA: ijkmp_android_set_volume(1.000000, 1.000000) 08-29 08:44:44.678 1605 1605 D IJKMEDIA: ffpipeline_set_volume 08-29 08:44:44.678 1605 1605 D IJKMEDIA: ijkmp_android_set_volume(1.000000, 1.000000)=void 08-29 08:44:44.682 914 1502 I DefaultMicInputStream: close 08-29 08:44:44.683 914 1502 I DefaultMicInputStream: hold recorder. 08-29 08:44:44.696 1605 1605 D IJKMEDIA: IjkMediaPlayer_setVolume 08-29 08:44:44.696 1605 1605 D IJKMEDIA: ijkmp_android_set_volume(1.000000, 1.000000) 08-29 08:44:44.696 1605 1605 D IJKMEDIA: ffpipeline_set_volume 08-29 08:44:44.696 1605 1605 D IJKMEDIA: ijkmp_android_set_volume(1.000000, 1.000000)=void 08-29 08:44:44.703 1605 1605 D IJKMEDIA: IjkMediaPlayer_setVolume 08-29 08:44:44.703 1605 1605 D IJKMEDIA: ijkmp_android_set_volume(1.000000, 1.000000) 08-29 08:44:44.703 1605 1605 D IJKMEDIA: ffpipeline_set_volume 08-29 08:44:44.704 1605 1605 D IJKMEDIA: ijkmp_android_set_volume(1.000000, 1.000000)=void 08-29 08:44:44.730 914 914 D SynthesizerService: get audio file:vf_speak_again_cn.mp3 08-29 08:44:44.760 914 4621 I player : vol 0.46666667, scale: 1.0 08-29 08:44:44.760 230 856 I MediaPlayerService: processName cmdline:/proc/914/cmdline 08-29 08:44:44.777 230 4622 D NuPlayerDriver: notifyListener_l(0xab5071e0), (1, 0, 0), loop setting(0, 0) 08-29 08:44:44.777 914 914 D MediaPlayer: setSubtitleAnchor in MediaPlayer 08-29 08:44:44.793 230 856 D NuPlayerDriver: start(0xab5071e0), state is 4, eos is 0 08-29 08:44:44.793 230 4622 I GenericSource: start 08-29 08:44:44.800 230 4627 I MediaCodec: callingProcessName:com.peasun.aispeech 08-29 08:44:44.800 230 4628 I MediaPlayerService: MediaPlayerService::getOMX 08-29 08:44:44.801 230 4628 I OMXClient: MuxOMX ctor 08-29 08:44:44.801 230 4628 I ACodec : onAllocateComponent:6752 mSoftCodecPref:0 componentName: 08-29 08:44:44.801 230 4628 I ACodec : onAllocateComponent:6783 mSoftCodecPref:0 componentName:OMX.google.mp3.decoder 08-29 08:44:44.801 227 714 I OMXMaster: makeComponentInstance(OMX.google.mp3.decoder) in mediacodec process 08-29 08:44:44.805 961 961 E FS : 充电状态 level:2 08-29 08:44:44.854 227 227 E OMXNodeInstance: setConfig(e30040:google.mp3.decoder, ConfigPriority(0x6f800002)) ERROR: Undefined(0x80001001) 08-29 08:44:44.854 230 4628 I ACodec : codec does not support config priority (err -2147483648) 08-29 08:44:44.855 230 4628 I MediaCodec: MediaCodec will operate in async mode 08-29 08:44:44.871 230 4626 D AudioTrack: Client defaulted notificationFrames to 2728 for frameCount 8184 08-29 08:44:44.872 230 4622 D NuPlayerDriver: notifyListener_l(0xab5071e0), (6, 0, 0), loop setting(0, 0) 08-29 08:44:44.877 476 624 W Binder : Binder call failed. 08-29 08:44:44.877 476 624 W Binder : java.lang.SecurityException: Neither user 1041 nor current process has android.permission.WAKE_LOCK. 08-29 08:44:44.877 476 624 W Binder : at android.app.ContextImpl.enforce(ContextImpl.java:1585) 08-29 08:44:44.877 476 624 W Binder : at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:1617) 08-29 08:44:44.877 476 624 W Binder : at com.android.server.power.PowerManagerService$BinderService.updateWakeLockWorkSource(PowerManagerService.java:3501) 08-29 08:44:44.877 476 624 W Binder : at com.android.server.power.PowerManagerService$BinderService.updateWakeLockUids(PowerManagerService.java:3492) 08-29 08:44:44.877 476 624 W Binder : at android.os.IPowerManager$Stub.onTransact(IPowerManager.java:105) 08-29 08:44:44.877 476 624 W Binder : at android.os.Binder.execTransact(Binder.java:565) 08-29 08:44:44.972 1068 1068 D TVManagerService: Manager Handler==== 08-29 08:44:45.929 961 961 E FS : 充电状态 level:2 08-29 08:44:46.724 914 914 D AsrRecogService: stop, needDestroy false 08-29 08:44:46.727 230 4627 I NuPlayerDecoder: [audio] saw output EOS 08-29 08:44:46.735 222 1150 I chatty : uid=1041(audioserver) AudioIn_26 expire 100 lines 08-29 08:44:46.746 914 914 D MsgController: clean by name:com.peasun.aispeech.sharjeck.AsrRecogService 08-29 08:44:46.747 476 624 W AppOps : Finishing op nesting under-run: uid 10040 pkg com.peasun.aispeech code 24 time=0 duration=0 nesting=0 08-29 08:44:46.761 914 914 D AIMonitorService: get asr status:asr.exit 08-29 08:44:46.762 914 914 D AirInputMethod: get asr status:asr.exit 08-29 08:44:47.047 961 961 E FS : 充电状态 level:2 08-29 08:44:47.300 230 4622 D NuPlayerDriver: notifyListener_l(0xab5071e0), (2, 0, 0), loop setting(0, 0) 08-29 08:44:47.301 230 2043 D NuPlayerDriver: reset(0xab5071e0) at state 6 08-29 08:44:47.301 230 2043 D NuPlayerDriver: notifyListener_l(0xab5071e0), (8, 0, 0), loop setting(0, 0) 08-29 08:44:47.320 230 4622 W AMessage: failed to post message as target looper for handler 0 is gone. 08-29 08:44:47.321 230 4622 D NuPlayerDriver: notifyResetComplete(0xab5071e0) 08-29 08:44:47.321 230 1757 D NuPlayerDriver: reset(0xab5071e0) at state 0 08-29 08:44:47.331 476 624 W Binder : Binder call failed. 08-29 08:44:47.331 476 624 W Binder : java.lang.SecurityException: Neither user 1041 nor current process has android.permission.WAKE_LOCK. 08-29 08:44:47.331 476 624 W Binder : at android.app.ContextImpl.enforce(ContextImpl.java:1585) 08-29 08:44:47.331 476 624 W Binder : at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:1617) 08-29 08:44:47.331 476 624 W Binder : at com.android.server.power.PowerManagerService$BinderService.updateWakeLockWorkSource(PowerManagerService.java:3501) 08-29 08:44:47.331 476 624 W Binder : at com.android.server.power.PowerManagerService$BinderService.updateWakeLockUids(PowerManagerService.java:3492) 08-29 08:44:47.331 476 624 W Binder : at android.os.IPowerManager$Stub.onTransact(IPowerManager.java:105) 08-29 08:44:47.331 476 624 W Binder : at android.os.Binder.execTransact(Binder.java:565) 分析这个日志,给出优化建议
08-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值