我想在android中提供服务,但我的服务没有运行?

时间:2014-08-21 14:00:31

标签: 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 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;
      }

}

我的BroadcastReceiver:

package com.example.service_m;

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

public class ServiceStarter extends BroadcastReceiver{

     @Override
        public void onReceive(Context _context, Intent _intent) {

           Intent service = new Intent(_context, ServiceCode.class );
           _context.startService(service);
        }


}

AndroidManifest

<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>

我的应用安装但在设备重启时没有运行。 有什么问题? 请帮我! 我想学习制作一个后台应用程序。

3 个答案:

答案 0 :(得分:2)

定义服务&amp;清单文件中的权限

    <service
        android:name="com.example.service_m.ServiceCode">
    </service>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

在接收器中使用此代码:

 Intent service = new Intent(_context, ServiceCode.class );
   _context.startService(service);

答案 1 :(得分:0)

问题出在这里

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

我认为它应该在下面(删除.class)

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

答案 2 :(得分:0)

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

首先,权限需要在清单中:

<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");

希望有所帮助。

相关问题