当前位置: 首页 > 知识库问答 >
问题:

Android服务无法启动

章翔宇
2023-03-14

好吧,这就是问题所在。我做context.startService(serviceIntent),但我没有从oncreateonstartCommand获得新的日志消息。

舱单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.distorted.flashnotifier"
    android:versionCode="0"
    android:versionName="0.6" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.distorted.flashnotifier.Home"
            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="FlashService"
            android:exported="true"
            android:enabled="true"
            android:permission="android.permission.READ_PHONE_STATE">
        </service>
    </application>
</manifest>
    null

活动代码:

private Context cont;

protected void onCreate(Bundle savedInstanceState) {
    cont = getApplicationContext();
}

public void setNotificator() {
    Intent serviceIntent = new Intent(cont, FlashService.class);
    if(callNotificator){
        cont.startService(serviceIntent);
        Log.d("LOG", "setNotificator works");
    } else {
        cont.stopService(serviceIntent);
    }
}

我试过什么:

  • 将所有“cont”更改为“this”
  • StartService(新意图(cont,FlashService.class));
package com.distorted.flashnotifier;

public class FlashService extends Service{

    public void OnCreate () {
        Log.d("LOG", "onCreate works");
    }

        public int OnStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}
    null

更新:

完整活动代码:

package com.distorted.flashnotifier;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TimePicker;
import android.widget.Toast;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;

public class Home extends Activity {

    private Button timeSelectBut1;
    private Button timeSelectBut2;
    private int hour1;
    private int hour2;
    private int minute1;
    private int minute2;
    static final int TIME_DIALOG_ID1 = 1;
    static final int TIME_DIALOG_ID2 = 2;
    private Switch callSwitch;
    private Switch notificationSwitch;
    private boolean callNotificator;
    private boolean notifNotificator;
    private Context cont;

//IMPORTANT UNTIL timeSelectBut1.setOnClickListener

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);
            cont = getApplicationContext();
        timeSelectBut1 = (Button) findViewById(R.id.selectTimeButton1);
        timeSelectBut1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID1);
            }
        });
        final Calendar cal1 = Calendar.getInstance();
        hour1 = cal1.get(Calendar.HOUR_OF_DAY);
        minute1 = cal1.get(Calendar.MINUTE);
        updateText(1);

        timeSelectBut2 = (Button) findViewById(R.id.selectTimeButton2);
        timeSelectBut2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                    showDialog(TIME_DIALOG_ID2);
                }
            });
            final Calendar cal2 = Calendar.getInstance();
            hour2 = cal2.get(Calendar.HOUR_OF_DAY);
            minute2 = cal2.get(Calendar.MINUTE);
            updateText(2);
    }

    @Override
    protected Dialog onCreateDialog(int id){
        if (id == 1) {
            return new TimePickerDialog(this, mTimeSetListener1, hour1, minute1, true);
        }
        if (id == 2) {
            return new TimePickerDialog(this, mTimeSetListener2, hour2, minute2, true);
        }
        return null;
    }

    public void updateText(int id) {
        if (id == 1) timeSelectBut1.setText(pad(hour1) + ":" + pad(minute1));
        if (id == 2) timeSelectBut2.setText(pad(hour2) + ":" + pad(minute2));
    }

    private TimePickerDialog.OnTimeSetListener mTimeSetListener1 = new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    hour1 = hourOfDay;
                    minute1 = minute;
                    updateText(1);
                }
            };
    private TimePickerDialog.OnTimeSetListener mTimeSetListener2 = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            hour2 = hourOfDay;
            minute2 = minute;
            updateText(2);
        }
    };

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

//IMPORTANT

    public void onCallSwitchClicked(View view) {
        boolean on = ((Switch) view).isChecked();
        if (on) {
            callNotificator = true;
            setNotificator();
            //timeSelectBut1.setText("callNotifTrue");
        } else {
            callNotificator = false;
            setNotificator();
            //timeSelectBut1.setText("callNotifFalse");
        }
    }

    public void onNotifSwitchClicked(View view) {
        boolean on = ((Switch) view).isChecked();
        if (on) {
            notifNotificator = true;
            //setNotificator();
        } else {
            notifNotificator = false;
            //setNotificator();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

//IMPORTANT

    public void setNotificator() {
        Intent serviceIntent = new Intent(this, FlashService.class);
        if(callNotificator){
            startService(serviceIntent);
            Log.d("LOG", "SetNotificator works");
        } else {
            stopService(serviceIntent);
        }
    }
}

共有1个答案

南门星河
2023-03-14

由于您是从活动启动服务,因此正确的代码是:

public void setNotificator() {
    Intent serviceIntent = new Intent(this, FlashService.class);
    if(callNotificator){
        startService(serviceIntent);
        Log.d("LOG", "setNotificator works");
    } else {
        stopService(serviceIntent);
    }
}

向前看,每个文档:系统在首次创建服务时调用此方法,以执行一次性安装过程(在调用onstartCommand()onbind()之前)。如果服务已经在运行,则不调用此方法。

因此,我最好将log调用放到onstartCommand

您的服务正在使用权限,也是来自文档:如果startService()bindService()stopService()的调用方没有被授予此权限,则方法将无法工作,Intent对象将无法传递给服务。如果未设置此属性,则 元素的权限属性设置的权限将应用于服务。如果两个属性都未设置,则服务不受权限保护。

 类似资料:
  • 问题内容: 码头工人给我一个困难时期。我按照以下说明进行操作,以便在运行由strato.de托管的Ubuntu 14.04的虚拟服务器上安装docker。 执行此行直接将我带入此错误消息: 安装完成后,我安装了上述两个软件包。现在我的问题是我无法让docker运行。 结果是: 结果是 和 结果是 有人对缺少哪些依赖项有任何线索吗?还有什么可能出问题了?码头工人是否提供任何日志? 我正在来回寻找解决

  • 当收到来自服务器的数据报文时,显示错误日志:

  • 我有一个spring boot可执行罐,装在数字海洋水滴中。我可以使用现在我想让我作为一个服务运行。 我已经创建了文件/etc/systemd/system/myapp。用这些内容服务 然后使其在系统启动时启动 我正在尝试启动服务 但是我得到了这个错误 无法启动myapp.service:未知单位:myapp.service详情请参阅系统日志和'systemctl statusmyapp.serv

  • 问题内容: 我在启动时无法启动服务。 我有一个广播接收器,只要设备启动(不是),就应该调用它,它会启动我的服务。不幸的是,该服务尚未启动! 我查看了此页面,阅读了所有答案,并按照每个步骤进行了操作……但是它仍然无法正常工作。每当手机重新启动/开机时,我都想启动我的服务。 但是我的服务仍然无法启动。这是我的清单: 在过去的三天中,我一直对此保持执着, 非常感谢您的帮助。为什么我的服务无法在启动时启动

  • 我使用AS 1.4已经有一段时间了,新的本机调试特性特别有用。 但是,本机调试无法在我的三星Galaxy Note 10.1 2014(Android 4.4)上运行。尝试启动本机调试会话时,我看到以下消息: 我还发现了一个悬而未决的问题:https://code.google.com/p/android/issues/detail?can=2 有办法解决这个问题吗?

  • 问题内容: 我知道,我不是第一个遇到此问题的人,但是我尝试了很多解决方案,但发现没有人起作用……也许您可以找到错误 错误(在没有.class和/.Client的情况下也会出现此错误,具体取决于其他设置) 12-02 16:40:15.359:W / ActivityManager(74):无法启动服务意图{act = com.android.fh.EnOceanApp.Client.class}: