我正在编写一个可以启用和禁用网络数据包连接的Android应用程序。我也使用一个广播接收器来检查网络数据包连接。我已经注册了广播接收器,并在Manifest文件中提供了所需的权限。但是,当我运行此应用程序时,它会更改连接状态,然后崩溃。但是,当我不包括这个广播接收器时,它工作正常。我无法看到任何可以提供一些线索的日志。
这是我的广播接收器代码。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rakesh.simplewidget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<!-- Permissions -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SimpleWidgetExampleActivity"
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=".ExampleAppWidgetProvider"
android:label="Widget ErrorBuster" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget1_info" />
</receiver>
-->
<receiver android:name=".ConnectivityReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
我的广播接收器类如下。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class ConnectivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = (NetworkInfo)intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(info.getType() == ConnectivityManager.TYPE_MOBILE){
if(info.isConnectedOrConnecting()){
Log.e("RK","Mobile data is connected");
}else{
Log.e("RK","Mobile data is disconnected");
}
}
}
}
我的主要活动文件。
package com.rakesh.simplewidget;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class SimpleWidgetExampleActivity extends Activity {
private Button btNetworkSetting;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btNetworkSetting = (Button)findViewById(R.id.btNetworkSetting);
if(checkConnectivityState(getApplicationContext())){
btNetworkSetting.setBackgroundColor(Color.GREEN);
}else{
btNetworkSetting.setBackgroundColor(Color.GRAY);
}
}
public void openNetworkSetting(View view){
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
Context context = view.getContext();
boolean enabled = !checkConnectivityState(context);
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try{
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
if(enabled){
Toast.makeText(view.getContext(), "Enabled Network Data", Toast.LENGTH_LONG).show();
view.setBackgroundColor(Color.GREEN);
}
else{
Toast.makeText(view.getContext(), "Disabled Network Data", Toast.LENGTH_LONG).show();
view.setBackgroundColor(Color.LTGRAY);
}
}catch(Exception e){
Log.e("Error", "some error");
Toast.makeText(view.getContext(), "It didn't work", Toast.LENGTH_LONG).show();
}
}
private boolean checkConnectivityState(Context context){
final TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
ConnectivityManager af ;
return telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED;
}
}
日志文件:
java.lang.RuntimeException: Unable to instantiate receiver com.rakesh.simplewidget.ConnectivityReceiver: java.lang.ClassNotFoundException: com.rakesh.simplewidget.ConnectivityReceiver in loader dalvik.system.PathClassLoader[/data/app/com.rakesh.simplewidget-2.apk]
E/AndroidRuntime(26094): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1777)
E/AndroidRuntime(26094): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
E/AndroidRuntime(26094): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
E/AndroidRuntime(26094): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(26094): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(26094): at android.app.ActivityThread.main(ActivityThread.java:3691)
E/AndroidRuntime(26094): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(26094): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(26094): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
E/AndroidRuntime(26094): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
E/AndroidRuntime(26094): at dalvik.system.NativeStart.main(Native Method)
似乎Android无法识别文件Broadcast Receiver类。知道我为什么会收到这个错误吗?
PS:有关Android环境和平台的一些信息。 - Android API 10。 - 在拥有android 2.3.6的三星Galaxy II上运行
编辑:
我的广播接收器文件ConnectivityReceiver.java存在于默认包中,但Android无法识别它。 Android正在当前包中查找此文件,即com.rakesh.simplewidget;
我刚刚将connectivityReciever.java文件移至com.rakesh.simplewidget
包,问题解决了。
答案 0 :(得分:1)
我的广播接收器文件ConnectivityReceiver.java
错误地出现在默认包中。 Android无法识别此文件,因为它正在当前包中搜索ConnectivityReceiver
。即com.rakesh.simplewidget;
这是一个简单的不明显的错误,导致我半个小时来调试它。
谢谢你们花时间阅读我的问题并提供你的意见。
更新了主要帖子。