手机启动时App Service无法启动

问题描述 投票:0回答:1

我在android studio中做了一个简单的服务,应该在我启动手机后立即启动。它应该至少显示一个TOAST消息。我使用Redmi note 4作为模拟器,当我启动或重启手机时,服务无法启动。我已将应用程序设置为自动启动设置。

Android Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dilip3.myapplication" >
    <!-- Permission for starting app on boot -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >

        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- Service required starting app on boot -->
        <service android:name=".MyService" android:label="My Service">
            <intent-filter>
                <action android:name="com.myapp.MyService" />
            </intent-filter>
        </service>

        <receiver
            android:enabled="true"
            android:name=".BootService"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.REBOOT"/>

                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>

        </receiver>
    </application>

</manifest>

boot service.Java

package com.example.dilip3.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class BootService extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
            Toast.makeText(context, "Boot Completed", Toast.LENGTH_SHORT).show();
            Intent serviceIntent = new Intent(context, MyService.class);
            context.startActivity(serviceIntent);
        }
    }
}

my service.Java

package com.example.dilip3.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service starting", Toast.LENGTH_LONG).show();
        return super.onStartCommand(intent,flags,startId);
    }
}

MainActivity.java没有任何更改。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
java android
1个回答
0
投票

您需要启动服务,而不是活动。

context.startActivity(serviceIntent);

context.startService(serviceIntent);
© www.soinside.com 2019 - 2024. All rights reserved.