-1

你好,朋友,

我无法在我的设备中运行 Android 服务。它在模拟器中运行良好。 这是我的示例代码:请查看代码。

MyScheduleReceiver.java:

    package com.smpt.wi_ficonnector;
    import java.util.Calendar;
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;

    public class MyScheduleReceiver extends BroadcastReceiver {

// Restart service every 30 seconds
private static final long REPEAT_TIME = 1000 * 30;

@Override
public void onReceive(Context context, Intent intent) {
    AlarmManager service = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, MyStartServiceReceiver.class);
    PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
            PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar cal = Calendar.getInstance();
    // Start 30 seconds after boot completed
    cal.add(Calendar.SECOND, 30);
    //
    // Fetch every 30 seconds
    // InexactRepeating allows Android to optimize the energy consumption
    service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
            cal.getTimeInMillis(), REPEAT_TIME, pending);

    // service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
    // REPEAT_TIME, pending);

}
}

我的服务.java:

这个类调用wifiservice类。这将启动服务。

    package com.smpt.wi_ficonnector;
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;

    public class MyService extends Service {

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

MyStartServiceReceiver.java:

这个类调用我的服务类。该服务类将向用户显示通知。

    package com.smpt.wi_ficonnector;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;

    public class MyStartServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Intent service = new Intent(context, WifiService.class);
    context.startService(service);
}
}

WifiService.java:

此类用于检查 wifi 连接并每 30 秒发送一次通知。

包 com.smpt.wi_ficonnector;

    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Binder;
    import android.os.IBinder;

    public class WifiService extends Service {

private final IBinder mBinder = new MyBinder();

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub

    boolean networkStatus = haveNetworkConnection();
    ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    Notification not;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    if(networkStatus == true || netInfo.isConnected() == true){
         not = new Notification(R.drawable.on, "Wi-Fi Connector", System.currentTimeMillis());
    } else {
         not = new Notification(R.drawable.off, "Wi-Fi is not connector", System.currentTimeMillis());
    }


    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), Notification.FLAG_ONGOING_EVENT);        
    not.flags = Notification.FLAG_ONGOING_EVENT;
    if(networkStatus == true || netInfo.isConnected() == true){
        not.setLatestEventInfo(this, "Wi-Fi Connector" , "Wi-Fi is connected. You can download data.", contentIntent);
    } else {
        not.setLatestEventInfo(this, "Wi-Fi Connector" , "Wi-Fi is not connected. You can not download data.", contentIntent);
    }

    mNotificationManager.notify(1, not);

    return Service.START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

public class MyBinder extends Binder {
    WifiService getService() {
        return WifiService.this;
    }
}

private boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}
}

AndroidManifest.xml:

此清单文件告诉 android 应用程序在后台运行服务。它不会使用任何活动。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.smpt.wi_ficonnector"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="11" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<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.smpt.wi_ficonnector.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-->

    <service
        android:name=".WifiService"
        android:icon="@drawable/ic_launcher"
        android:label="@string/service_name" >
    </service>

    <service
        android:name="MyService"
        android:process=":meinprocess"
        android:icon="@drawable/ic_launcher"
        android:label="@string/service_name" >
    </service>

    <receiver android:name="MyScheduleReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver android:name="MyStartServiceReceiver" >
    </receiver>

</application>

我的代码在模拟器中运行良好。但它不适用于真实设备。请帮我找出问题所在。

4

1 回答 1

2

BOOT_COMPLETED从 Android 3.1 开始,除非用户至少运行了一次应用程序,否则您将不会收到Intent。由于清单中没有活动,因此用户无法运行您的应用程序。这不适用于运行 Android 3.1 或更高版本的设备。

请参阅http://developer.android.com/about/versions/android-3.1.html上的“在已停止的应用程序上启动控件”

于 2013-03-05T15:29:50.307 回答