Vig*_*ala 2 android broadcastreceiver android-manifest android-wifi
我一直在使用广播接收器开发一个没有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>
Run Code Online (Sandbox Code Playgroud)
这是我的Broadcastreceiver类:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
您将需要为服务创建一个虚拟活动,该虚拟活动将onCreate()在虚拟对象中触发,可能是非UI用户finish().
如果没有这个,就不可能实现所需的实现,尤其是Android 3.1以上.
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
在应用程序启动时仅运行后台服务启动
没有活动的android应用程序
http://commonsware.com/blog/2011/07/13/启动完成回归,confirmed.html
有关服务的更多信息:http:
//developer.android.com/guide/components/services.html
https://developer.android.com/training/run-background-service/create-service.html
http:// www.vogella.com/tutorials/AndroidServices/article.html
| 归档时间: |
|
| 查看次数: |
9977 次 |
| 最近记录: |