无法让android服务在启动时启动

时间:2017-06-12 13:45:43

标签: android service boot

我有启动服务器的启动接收器,我也可以在应用程序内启动和停止服务。

但是当我启动手机时,服务确实启动但在启动后立即停止。我已将onStartCommand的返回值设置为START_STICKY。

这是我的启动接收器:

public void onReceive(final Context context, final Intent intent) {
    if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
        Toast.makeText(context, "Boot received", Toast.LENGTH_LONG).show();

        this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        this.mTelephonyMgr     = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        this.encryption        = new Encryption(mTelephonyMgr, sharedPreferences);

        //startService(context);
        Intent i = new Intent(context, ProfileActivity.class);
        i.putExtra("boot_received", false);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

        sharedPreferences.edit().putBoolean(Keys.AVAILABILITY, false).apply();
    }
}


public void startService(final Context context){
    final Intent intent = new Intent(context, PhoneCallService.class);

    // If no token, try getting one.
    if (Keys.phoneServiceToken == null || Keys.phoneServiceToken.isEmpty()) {
        getToken(context, new Callbacks.ReceivePhoneServiceTokenCallback() {
            @Override
            public void receivePhoneServiceTokenCallback(boolean gotToken) {

                if (gotToken) {
                    intent.putExtra(Keys.PHONE_SERVICE_TOKEN_EXTRA, Keys.phoneServiceToken);
                    intent.setAction(PhoneCallService.START_SERVICE);
                    context.startService(intent);
                } else {
                    Log.d(LOG_TAG, String.valueOf(R.string.unexpected_error));
                }
            }
        });
    } else {
        intent.putExtra(Keys.PHONE_SERVICE_TOKEN_EXTRA, Keys.phoneServiceToken);
        intent.setAction(PhoneCallService.START_SERVICE);
        context.startService(intent);
    }
}

public void getToken(Context context, final Callbacks.ReceivePhoneServiceTokenCallback callback){
    final String API = Keys.getpvmURL() + SharedResources.URL_DIRECT_CALL_TOKEN;
    JsonObject json = encryption.getID();
    json.addProperty("versionCode", BuildConfig.VERSION_CODE);

    Ion.with(context)
            .load(API)
            .setTimeout(Keys.TIMEOUT_DIRECT_CALL_TOKEN)
            .setJsonObjectBody(json)
            .asJsonObject()
            .withResponse()
            .setCallback(new FutureCallback<Response<JsonObject>>() {
                @Override
                public void onCompleted(Exception e, Response<JsonObject> result) {
                    if(e == null) {
                        Log.d(LOG_TAG, "No Exceptions");
                        if(result.getHeaders().code() == 200) {
                            if(result.getResult().has("result")) {
                                Keys.phoneServiceToken = result.getResult().get("result").getAsString();
                                callback.receivePhoneServiceTokenCallback(true);
                            } else {
                                Log.w(LOG_TAG, "Does not have result");
                                callback.receivePhoneServiceTokenCallback(false);
                            }
                        } else {
                            Log.w(LOG_TAG, "Not getting 200 " + result.getHeaders().message());
                            callback.receivePhoneServiceTokenCallback(false);
                        }
                    } else {
                        Log.e(LOG_TAG, "Exception has occurred " + e.getClass());
                        callback.receivePhoneServiceTokenCallback(false);
                    }
                }
            });

这是我的onStart和onCreate方法:

@Override
public void onCreate() {
    Log.d(LOG_TAG, "Service started");

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(PhoneCallService.this);
    notificationManager = (NotificationManager) PhoneCallService.this.getSystemService(Context.NOTIFICATION_SERVICE);

    voiceBroadcastReceiver = new VoiceBroadcastReceiver();
    registerReceiver();

    audioManager = (AudioManager) PhoneCallService.this.getSystemService(Context.AUDIO_SERVICE);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if(intent == null || intent.getAction() == null) {
        Log.d(LOG_TAG, "No Action");
    }
    else if (intent.getAction().equals(START_SERVICE)) {
        Log.d(LOG_TAG, "Starting PhoneCallService");
        accessToken = intent.getStringExtra(Keys.PHONE_SERVICE_TOKEN_EXTRA);
        Log.d(LOG_TAG, accessToken);
        if (accessToken != null && !accessToken.isEmpty()) {
            registerForCallInvites();
            MyFcmListenerService.availableToCall = true;
        } else {
            stopSelf();
            MyFcmListenerService.availableToCall = false;
        }
    }
    else if (intent.getAction().equals(STOP_SERVICE)) {
        stopSelf();
        MyFcmListenerService.availableToCall = false;
    }
    else if (intent.getAction().equals(ACTION_INCOMING_CALL)) {
        handleIncomingCallIntent(intent);
    }
    else {
        Log.d(LOG_TAG, intent.getAction());
    }

    return START_STICKY;
}

出于某种原因,我无法让它处理启动请求。

0 个答案:

没有答案