安装(重启)后安卓apk运行?

时间:2016-10-10 21:14:50

标签: android android-activity android-service android-manifest android-broadcastreceiver

您好我正在尝试让我的Android应用程序(它没有UI,它只是应该是后台服务)在安装时运行,或者至少在重启设备时运行。这是我到目前为止的工作,任何人都有任何想法为什么它不会启动安装apk并重新启动设备?

Android清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="gitlab.project" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="@string/app_name" >
    <activity
        android:name="gitlab.project.MainActivity"
        android:label="@string/app_name"
        android:theme = "@android:style/Theme.NoDisplay">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </activity>

    <receiver
            android:name="gitlab.project.AutoStart"
            android:enabled="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
    <service android:name="gitlab.project.AlphaService"
             android:enabled="true"
             android:exported="true" />
</application>
</manifest>

MainActivity:

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, AlphaService.class);
    this.startService(intent);
}
}

AlphaService:

public class AlphaService extends Service implements
    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private Profiler profiler;
private AlphaApiClient alphaApiClient;
private GoogleApiClient googleApiClient;
private int batteryHealth;

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

@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
    batteryHealth = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 2);

    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }
    googleApiClient.connect();

    return Service.START_STICKY;
}

@Override
public void onConnected(Bundle connectionHint) {
    ProcService procService = new DefaultProcService();
    SystemInformationService systemInformationService = new SystemInformationService(googleApiClient, getContentResolver());
    profiler = new Profiler(procService, systemInformationService);
    alphaApiClient = new AlphaApiClient(getString(R.string.USERNAME), getString(R.string.PASSWORD));
    Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
            Log.d("Alpha service", "Profiling system info...");
            AndroidSystem androidSystem = profiler.profile(batteryHealth);
            String url = getString(R.string.API_URL);
            alphaApiClient.doPostRequest(androidSystem, url);
        }
    });
    thread.start();

    //Stop service once it finishes its task
    stopSelf();
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onDestroy() {
    AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarm.set(
            AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + (4000 * 60 * 60),
            PendingIntent.getService(this, 0, new Intent(this, AlphaService.class), 0)
    );
}
}

AutoStart(广播接收器):

public class AutoStart extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent(context, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);

    context.startService(new Intent(context, AlphaService.class));
}
}

我的后台服务无法运行的任何想法?我可以使用adb运行它,但在尝试从手机/平板电脑手动安装时却无法运行。任何帮助是极大的赞赏。

2 个答案:

答案 0 :(得分:1)

除非处于启动状态,否则应用程序不会运行任何服务或广播接收器。它是从发射器或通过ADB运行后处于启动状态。

1)开展一项活动,a)开始您的服务,b)禁用自己(How to enable and disable a component?)。

2)制作一个启动接收器(Android BroadcastReceiver on startup - keep running when Activity is in Background)。

您的启动接收器已启动该服务,它不需要启动该活动。安装后只需启动一次活动。这是正确的设置。

的AndroidManifest.xml

<activity
    android:name="gitlab.project.MainActivity"
    android:theme = "@android:style/Theme.NoDisplay">
    <intent-filter>
        <!-- Activity needs to show up in launcher. -->
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<receiver
        android:name="gitlab.project.AutoStart"
        android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

AutoStart.java

public class AutoStart extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) {        
            context.startService(new Intent(context, AlphaService.class));
        }
    }
}

答案 1 :(得分:0)

我认为问题出在您的AutoStart类中:

Intent i = new Intent(context, MainActivity.class);

使用MainActivity打包您的意图。尝试将其更改为AlphaService。