由BootReceiver启动时,Android服务表现得很奇怪

时间:2016-11-13 18:57:37

标签: java android android-manifest android-permissions

我有一项服务,我想用它来远程管理我的设备。当它启动时,它应该连接到服务器,然后从那里接收命令。当我正常启动我的应用程序时,我使用

启动MainActivity我的服务

startService(new Intent(this, service.class));

这完全没问题。但是,当autostart.class启动服务时,它会报告它无法解析主机。所以我只是让它每秒重试一次,直到它连接,但即使网络连接正常并且工作正常,也永远无法连接。当我点击应用程序图标然后,它会杀死旧服务,启动一个新服务,连接,一切都很好,但不是自动启动。这项服务在启动时没有任何权限,但我对此并不确定。

autostart.class

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class autostart extends BroadcastReceiver 
{
     @Override
     public void onReceive(Context context, Intent intent) {
      if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
       Intent pushIntent = new Intent(context, service.class);
       pushIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startService(pushIntent);
       Log.i("Boot Receiver", "Service started");
      }
     }

}

的AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="21"
        android:targetSdkVersion="25" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".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>
        <activity
            android:theme="@style/Theme.Transparent"
            android:name=".DialogActivity"
            android:label="@string/app_name" >
        </activity>  
        <receiver android:name=".autostart">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver> 
        <receiver
            android:name=".Darclass"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/my_admin" />

            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>
        </receiver>
        <service android:enabled="true" android:isolatedProcess="false" android:persistent="true" android:name=".service" />
    </application>
</manifest>

1 个答案:

答案 0 :(得分:0)

一般情况下,我会检查与此连接(虽然不推荐使用getAllNetworkInfo)

 public boolean areWeConnected(){
    /** 
     * isConnected()
     * Indicates whether network connectivity exists and it is possible to establish connections and pass data.
     * Always call this before attempting to perform data transactions.
     */
     boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

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