android - 为什么我在 android 启动时无法启动我的服务? (附代码)

标签 android android-service

我正在尝试编写简单的基本应用程序,以便在手机启动时启动我的服务。

我添加了我需要的所有权限。

我在我的 android ( android 6.01 ) 上安装了应用程序。 当我重新启动手机时 - 我看不到服务已启动或未执行任何操作。

  1. 为什么这不起作用?
  2. 如何在 android 上调试服务?

代码:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAG" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">

    <service android:name=".MyService" android:enabled="true" android:exported="true"/>

    <receiver android:name=".MainBroadcastReceiver" android:enabled="true" android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>
</application>



public class MainBroadcastReceiver extends BroadcastReceiver {
    public MainBroadcastReceiver(){

    }

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




public class MyService extends Service {

private File _file;
private Timer _timer;
private FileOutputStream _fileOutputStream;

public MyService() throws IOException {

    _file = new File("/sdcard/" + "myServiceText.txt");
    _file.createNewFile();

    _fileOutputStream = openFileOutput(_file.getName(), Context.MODE_APPEND);
    _fileOutputStream.write("Ctor called".getBytes());
}

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


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

    _timer = new Timer("timer");
    _timer.schedule(new TimerTask()
    {
        @Override
        public void run()
        {
            try {
                _fileOutputStream.write("onCreate Called".getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, 0, 5000);

}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        _fileOutputStream.write("onStartCommand".getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return START_STICKY;
}

最佳答案

您的代码没有问题,您的服务未运行的原因是您的应用处于停止状态

1. Stopped state是一种安全程序,仅当用户首次从主屏幕手动启动应用程序后,它才允许您的应用程序“自行唤醒”。( Unless you are a system )
通过这样做,android 可以防止恶意应用程序在您不知情的情况下在您的手机后台安装和运行。

2. 如何调试后台进程?
在处理后台进程时,您需要两个很棒的工具 -
首先是 adb broadcast 命令 -
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

第二个是附加远程调试器 (Android Studio):
运行 |将调试器附加到 android 进程(菜单中的最后一个选项)|选择您的应用进程。

关于android - 为什么我在 android 启动时无法启动我的服务? (附代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38806215/

相关文章:

android - 如何使最近搜索建议列表看起来像 Material 设计规范中的那样?

java - 让后台服务始终运行并且不会自杀或系统破坏它

android - 在 android 中杀死我的应用程序后,Job Scheduler 不工作?

android - 屏幕旋转后获取服务

android - 单击后退按钮,MediaPlayer 崩溃 Android

android - 已经启动的Android Service可以接收Intent形式的NFC标签吗?

javascript - Chrome/Android杀死了后台运行的websocket

Android Preferenceactivity getView

android - android kitkat 版本中的 javax.net.ssl.SSLException : Connection closed by peer.

android - 水平recyclerview内部垂直recyclerview滚动性能