我正在编写一个可以启用和禁用网络数据包连接的 Android 应用程序。我还使用一个广播接收器来检查网络数据包连接。我已经注册了广播接收器并在清单文件中提供了所需的权限。但是当我运行这个应用程序时,它会改变连接状态,然后它就会崩溃。但是当我不包括这个广播接收器时,它工作正常。我看不到任何可以提供一些线索的日志。
这是我的广播接收器代码。
<?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 无法识别文件广播接收器类。知道为什么我会收到此错误吗?
PS:关于Android环境和平台的一些信息。- Android API 10. - 在具有 android 2.3.6 的三星 Galaxy II 上运行
编辑:
我的广播接收器文件 ConnectivityReceiver.java 存在于默认包中,Android 无法识别它。Android 正在当前包中寻找这个文件,即com.rakesh.simplewidget;
我刚刚将connectivityReciever.java 文件移动到com.rakesh.simplewidget
包中,问题就解决了。