【发布时间】:2014-02-14 06:39:01
【问题描述】:
我一直在使用广播接收器开发一个没有 UI 的简单应用程序。
该应用不包含任何活动。
我已授予必要的权限。
我从这个网址获取代码:http://developerandro.blogspot.in/2013/09/check-internet-connection-using.html
当我点击更改 wifi 状态时,应用程序显示“未连接到互联网”。它工作正常。
但我的问题是在我的清单文件中注册了一个我没有的活动。所以我从我的清单中删除了这些行。然后没有显示敬酒,我也检查了日志。更改 wifi 状态时没有输出。
为什么会这样?请大家帮帮我...
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcast_internetcheck"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broadcast_internetcheck.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.example.broadcast_internetcheck.NetworkChangeReceiver"
android:label="NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
这是我的广播接收器类:
public class NetworkChangeReceiver extends BroadcastReceiver{
@Override
public void onReceive(final Context context, final Intent intent) {
String status = NetworkUtil.getConnectivityStatusString(context);
/*Above line will return the status of wifi */
Toast.makeText(context, status, Toast.LENGTH_LONG).show();
}
}
【问题讨论】:
-
你的意思是你无法连接到互联网??是这样吗?
-
那不是我的情况,当我改变我的 wifi 状态时,我需要一个简单的祝酒词。
-
所以你想做吐司按钮,我没有你的代码,但我会为你做一个简单的吐司按钮?
-
你的问题,是因为activity自带显示Toast的功能,当activity没有启动时,是不会显示Toast的。您可以编写一个服务来执行此操作而无需任何活动。
-
@user2652394 toast 显示在 BroadcastReceiver 类的 onReceive 方法中。
标签: android broadcastreceiver android-manifest android-wifi