Java 我想用安卓系统提供服务,但我的服务没有运行?

Java 我想用安卓系统提供服务,但我的服务没有运行?,java,android,service,Java,Android,Service,我的应用程序已安装,但重新启动时设备未运行 我的服务: package com.example.service_m; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.widget.Toast; public class ServiceCode extends Service{ @Override public IBinde

我的应用程序已安装,但重新启动时设备未运行

我的服务:

package com.example.service_m;

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

public class ServiceCode extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
        //TODO do something useful

          for(int i=0;i<=10;i++){
              Toast.makeText(getApplicationContext(), "hello world", 
                       Toast.LENGTH_LONG).show();
              try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          }

        return Service.START_NOT_STICKY;
      }

}
雄激素单

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.service_m.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
                <receiver android:name="com.example.service_m.ServiceStarter" >
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
            <service
    android:name="com.example.service_m.ServiceCode.class">
    </service>
    </application>

我的应用程序已安装,但在设备重新启动时未运行。 问题是什么? 请帮帮我!
我想学习制作后台应用。

在清单文件中定义服务和权限

    <service
        android:name="com.example.service_m.ServiceCode">
    </service>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
问题就在这里

<service
android:name="com.example.service_m.ServiceCode.class">
</service>

我想应该在下面(remove.class)


非常接近,但您错过了几件事:

首先,许可需要在清单中:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service
    android:name="com.example.service_m.ServiceCode"
    android:exported="false"
    android:label="ServiceCode" />
最后一件事,尝试使用日志记录而不是toast进行调试,通常会更有用:)


希望这能有所帮助。

为什么您要在onstart命令下执行此任务?只需简单地重写onCreate方法,并在此方法中执行任何您想要的操作。清单中没有列出您的服务。为什么要使用com.example.Service\m.ServiceCode.class?
<service
android:name="com.example.service_m.ServiceCode">
</service>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service
    android:name="com.example.service_m.ServiceCode"
    android:exported="false"
    android:label="ServiceCode" />
android:enabled="true"
Log.i("Service Starter", "Hello WOrld");