0

我想在手机启动时启动一项服务,但无法正常工作,我不明白为什么。在我的包中,我创建了一个类

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

    public class BootPhone extends BroadcastReceiver{

        Intent intent;

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            intent = new Intent(context, Service.class);
            context.startService(intent);
        }

    }

我的 Service.class 是

public class Service extends Service{

    Notify notify;


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


    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        notify = new Notify(this);
    }

}

在我的清单中我放了

<service android:name="Servicer"></service>
    <receiver android:name="BootPhone"
                android:enabled="true"
                        android:exported="false" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
                    </intent-filter>
    </receiver>

但是,为什么它不起作用?问题/错误在哪里?

4

1 回答 1

2

您必须在清单中声明用户权限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
于 2013-08-01T04:50:09.877 回答