在Nexus 7广播接收器 [英] Broadcast Receiver on Nexus 7

查看:134
本文介绍了在Nexus 7广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写在手机开机运行,必须关闭SD卡读取数据的服务。起初,我使用的是reciever为 android.intent.action.BOOT_COMPLETED ,但切换到下面的意图,以确保SD卡已经被载入。

我的问题是,在我的Nexus 7,它似乎并没有收到 MEDIA_MOUNTED 意图。使用Nexus 7没有SD卡(但它有独立的SD卡分区)。我也试过 BOOT_COMPLETED 的意图,用同样的运气。我已经测试的仿真器和迅雷我一样code,两者的意图工作。

Manifiest:

 <接收
    机器人:名字=。Startu preceiver
    机器人:启用=真
    机器人:出口=真
    机器人:标签=启动NFS挂载服务>    &所述;意图滤光器>
        <作用机器人:名字=android.intent.action.MEDIA_MOUNTED>< /作用>
        <数据机器人:计划=文件/>
        !< - <作用机器人:名字=android.intent.action.BOOT_COMPLETED>< /作用> - >
    &所述; /意图滤光器>
< /接收器>

广播接收器类:

 公共类Startu preceiver延伸广播接收器
{
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图)
    {
        //如果(android.intent.action.BOOT_COMPLETED.equals(intent.getAction()))
        //如果(android.intent.action.MEDIA_MOUNTED.equals(intent.getAction()))
        // {            Log.d(NFS_Automounter,摩收到);
            意图serviceIntent =新意图(com.ancantus.nfsautomounter.AutomountService);
            context.startService(serviceIntent);
        //}
    }
}

我注释掉的意图匹配只是尝试,如果在所有执行类日志。

我唯一的预感是,的Nexus 7不广播 MEDIA_MOUNTED ,因为它没有一个真正的SD卡;但我不能接受 BOOT_COMPLETED 意图无论是。

和以福斯代尔的问题;是的,我确实有 BOOT_COMPLETED 的权限。

 <使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/>


解决方案

多少次我必须在此答案类型它开始上来了足够的搜索结果,人们会发现它之前?也许黑体字上限将工作:

与Android 3.1开始,NO 广播接收器将工作,不惜一切,直到一些手工的运行一个应用程序的其他组件,如一个用户运行活动

这是在文档(尽管不是地理位置优越)在博客文章的,而且在许多计算器解答如:

所以,一个活动添加到您的应用程序。你需要一些活动,无论如何,对于设置来控制你的后台操作,为您的文档,为您的许可协议,为您的隐私政策,等等。

(注意:我真的不大喊大叫你 - 我只是沮丧,这样下去上来尽管努力得到了这个词......)

I am trying to write a service that runs on phone boot, and must read data off the SD card. At first I was using a reciever for android.intent.action.BOOT_COMPLETED but switched to the intent below to make sure that the SD card has been loaded.

My Issue is that on a my Nexus 7, it doesn't appear to receive the MEDIA_MOUNTED intent. The Nexus 7 doesn't have an SD card (but it has separate SD card partition). I also tried the BOOT_COMPLETED intent, with the same luck. I have tested the same code on the emulator and my Thunderbolt, and both intents work.

Manifiest:

<receiver
    android:name=".StartupReceiver"
    android:enabled="true"
    android:exported="true"
    android:label="Start the NFS Automounter Service">

    <intent-filter>
        <action android:name="android.intent.action.MEDIA_MOUNTED"></action>
        <data android:scheme="file"/> 
        <!-- <action android:name="android.intent.action.BOOT_COMPLETED"></action>-->
    </intent-filter>
</receiver>

The BroadcastReceiver class:

public class StartupReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) 
        //if ("android.intent.action.MEDIA_MOUNTED".equals(intent.getAction()))
        //{

            Log.d("NFS_Automounter", "Recieved Mount");
            Intent serviceIntent = new Intent("com.ancantus.nfsautomounter.AutomountService");
            context.startService(serviceIntent);
        //}
    }
}

I commented out the intent matching just to try and log if the class is executed at all.

My only hunch is that the Nexus 7 doesn't broadcast a MEDIA_MOUNTED because it doesn't have a real SD card; but I can't receive the BOOT_COMPLETED intent either.

And to forstall the question; yes I do have the BOOT_COMPLETED permission.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

解决方案

How many times must I type in this answer before it starts coming up in enough search results that people will find it? Maybe boldface caps will work:

STARTING WITH ANDROID 3.1, NO BroadcastReceiver WILL WORK, AT ALL, UNTIL SOMETHING HAS MANUALLY RUN ONE OF THE APPLICATION'S OTHER COMPONENTS, SUCH AS A USER RUNNING AN ACTIVITY.

This is in the documentation (albeit not well located), in blog posts, and in many StackOverflow answers, such as:

So, add an activity to your app. You need some activities anyway, for settings to control your background operation, for your documentation, for your license agreement, for your privacy policy, etc.

(note: I'm not really yelling at you -- I am just frustrated that this keeps coming up despite efforts to get the word out...)

这篇关于在Nexus 7广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆