在IntentService上没有调用onHandleIntent方法

时间:2016-04-29 20:25:22

标签: android

我正在开发一个Android应用程序,当设备连接到互联网时启动服务。 问题是我的IntentService子类中没有调用onHandleIntent方法。 有人可以帮我解释一下原因吗?

以下是我的IntentService子类:

public class RefreshService extends IntentService {
static final String TAG = "REFRESH_SERVICE";

public RefreshService() {
    super("RefreshService");
}

@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(RefreshService.this, "onCreate", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onCreate");
}

@Override
protected void onHandleIntent(Intent intent) {
    Toast.makeText(RefreshService.this, "onHandleIntent", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onHandleIntent");
}

@Override
public void onDestroy() {
    Toast.makeText(RefreshService.this, "onDestroy", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onDestroy");
    super.onDestroy();
}
}

以下是我的BroadcastReceiver子类:

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    String status = NetworkUtil.getConnectivityStatusString(context);
    Toast.makeText(context, status, Toast.LENGTH_LONG).show();
    //
    int statusNumber = NetworkUtil.getConnectivityStatus(context);
    if (statusNumber == 1 || statusNumber == 2) {
        context.startService(new Intent(context, RefreshService.class));
    }
}
}

这是我的AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <service android:name=".RefreshService" />

    <receiver android:name=".MyBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


</application>

这是我的NetworkUtil类:

public class NetworkUtil {

public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 2;
public static int TYPE_NOT_CONNECTED = 0;


public static int getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null != activeNetwork) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return TYPE_WIFI;

        if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            return TYPE_MOBILE;
    }
    return TYPE_NOT_CONNECTED;
}

public static String getConnectivityStatusString(Context context) {
    int conn = NetworkUtil.getConnectivityStatus(context);
    String status = null;
    if (conn == NetworkUtil.TYPE_WIFI) {
        status = "Wifi enabled";
    } else if (conn == NetworkUtil.TYPE_MOBILE) {
        status = "Mobile data enabled";
    } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
        status = "Not connected to Internet";
    }
    return status;
}
}

显示的Toast消息序列为: (当用wifi连接到互联网时)

未连接到互联网
  启用Wifi
  的onCreate
  onDestroy

我的问题是为什么onHandleIntent消息没有显示???

0 个答案:

没有答案