java.lang.RuntimeException: Error receiving broadcast Intent
发送广播的时候报 Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x8000010 (has extras)。找不到问题,所以来求助。
package com.keyli.service;
import com.jikexueyuan.tulingdemo.MainActivity;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class NetworkStateService extends Service{
private ConnectivityManager connectivityManager;
private NetworkInfo info;
private static final String tag="JsonUtils";
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Intent myintent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Bundle mybundle=new Bundle();
connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
info = connectivityManager.getActiveNetworkInfo();
if(info != null && info.isAvailable()) {
Log.d(tag, "当前网络名称:" + info.getTypeName());
mybundle.putString("name", "true");
} else {
Log.d(tag, "当前没有网络");
mybundle.putString("name", "false");
}
intent.putExtras(mybundle);
sendBroadcast(myintent);
}
}
};
@Override
public void onCreate() {
super.onCreate();
IntentFilter mFilter = new IntentFilter();
mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mReceiver, mFilter);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
08-07 14:43:56.440: E/linker(30295): load_library(linker.cpp:760): library "libmaliinstr.so" not found
08-07 14:43:56.444: E/(30295): appName=com.jikexueyuan.tulingdemo, acAppName=com.android.cts.openglperf
08-07 14:43:56.444: E/(30295): 0
08-07 14:43:56.444: E/(30295): appName=com.jikexueyuan.tulingdemo, acAppName=com.android.browser
08-07 14:43:56.444: E/(30295): 0
08-07 14:43:56.482: E/AndroidRuntime(30295): FATAL EXCEPTION: main
08-07 14:43:56.482: E/AndroidRuntime(30295): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x8000010 (has extras) } in com.keyli.service.NetworkStateService$1@4257dad8
08-07 14:43:56.482: E/AndroidRuntime(30295): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:801)
08-07 14:43:56.482: E/AndroidRuntime(30295): at android.os.Handler.handleCallback(Handler.java:800)
08-07 14:43:56.482: E/AndroidRuntime(30295): at android.os.Handler.dispatchMessage(Handler.java:100)
08-07 14:43:56.482: E/AndroidRuntime(30295): at android.os.Looper.loop(Looper.java:194)
08-07 14:43:56.482: E/AndroidRuntime(30295): at android.app.ActivityThread.main(ActivityThread.java:5469)
08-07 14:43:56.482: E/AndroidRuntime(30295): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 14:43:56.482: E/AndroidRuntime(30295): at java.lang.reflect.Method.invoke(Method.java:525)
08-07 14:43:56.482: E/AndroidRuntime(30295): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:857)
08-07 14:43:56.482: E/AndroidRuntime(30295): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
08-07 14:43:56.482: E/AndroidRuntime(30295): at dalvik.system.NativeStart.main(Native Method)
08-07 14:43:56.482: E/AndroidRuntime(30295): Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.net.conn.CONNECTIVITY_CHANGE from pid=30295, uid=10076
08-07 14:43:56.482: E/AndroidRuntime(30295): at android.os.Parcel.readException(Parcel.java:1425)
08-07 14:43:56.482: E/AndroidRuntime(30295): at android.os.Parcel.readException(Parcel.java:1379)
08-07 14:43:56.482: E/AndroidRuntime(30295): at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2202)
08-07 14:43:56.482: E/AndroidRuntime(30295): at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1182)
08-07 14:43:56.482: E/AndroidRuntime(30295): at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:338)
08-07 14:43:56.482: E/AndroidRuntime(30295): at com.keyli.service.NetworkStateService$1.onReceive(NetworkStateService.java:40)
08-07 14:43:56.482: E/AndroidRuntime(30295): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:791)
08-07 14:43:56.482: E/AndroidRuntime(30295): ... 9 more