Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为广播接收器显示土司_Android_Broadcastreceiver_Toast - Fatal编程技术网

Android 为广播接收器显示土司

Android 为广播接收器显示土司,android,broadcastreceiver,toast,Android,Broadcastreceiver,Toast,我正在学习广播接收机 我想在SD卡安装或卸载时显示Toast消息 我遵循了SO上提供的几个示例,并编写了以下代码。但是,当我挂载/卸载SD卡(设置->卸载SD卡)时,不会显示消息“Hello there” 谢谢你的帮助 <receiver android:name="MountReceiver" android:enabled="true" android:exported="true"> <intent-filter> &

我正在学习广播接收机

我想在SD卡安装或卸载时显示Toast消息

我遵循了SO上提供的几个示例,并编写了以下代码。但是,当我挂载/卸载SD卡(设置->卸载SD卡)时,不会显示消息“Hello there”

谢谢你的帮助

<receiver
    android:name="MountReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <data android:scheme="file" />
    </intent-filter>
</receiver>
您可以这样做:

@Override
public void onReceive
        (Context context, Intent intent) {
    //Use application context
    Toast.makeText(context, "Hello world", Toast.LENGTH_SHORT).show();

}

正如我在评论部分提到的,在
之外使用
标记太愚蠢了。不知何故,我在24个多小时里都没有意识到这一点。我希望这对像我这样的新手将来有所帮助。

请检查您的清单文件

<receiver
    android:name="<package>.MountReceiver"">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <data android:scheme="file" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
        <data android:scheme="file" />
    </intent-filter>
</receiver>

请随时告诉我你的结果

谢谢你的回复。我按照建议将“MainActivity.this”更改为“context”,但它仍然不起作用。请尝试放置日志,看看是否调用过onReceive。如果没有,请发布您的意图。谢谢,我们将尝试添加日志,并查看其是否被调用。我的意图?你指的是代码吗?我只有上面清单文件中提到的“意图过滤器”。若你们并没有提到代码,我正在学习应用程序开发,想学习广播接收器的任何动作,先静态,然后动态。添加日志,onReceive本身并没有被调用。我是否需要提供接收器的安卓:名称与绝对路径不同?谢谢帮助。在几乎撞到我的头超过24小时后。找到了答案,我在清单xml中使用了外部标记。愚蠢,愚蠢的我!亲爱的投票人,请告诉我投票失败的原因。这样我以后就可以避免那个错误了谢谢!
<receiver
    android:name="<package>.MountReceiver"">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <data android:scheme="file" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
        <data android:scheme="file" />
    </intent-filter>
</receiver>
public class MountReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
         // react to the event
       Toast.makeText(context, "Hello there", Toast.LENGTH_SHORT).show();
    }
}