服务和启动代码不起作用

时间:2015-10-28 08:38:40

标签: android android-intent android-service

我正在尝试创建一个将在启动时运行的服务,但是当在我的模拟器中尝试时,日志没有通过日志标记显示我的消息,所以显然有些错误。

这是我的代码。

service.java

public class service extends Service {


private static final String TAG = "myapp.mycomp";

public service() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "Service started");

    Runnable r = new Runnable() {
        @Override
    public void run() {
/** something to do **/

        }
    };

    Thread service = new Thread(r);
    service.start();
    return Service.START_STICKY;
    }

@Override
public void onDestroy() {
    Log.i(TAG, "Service stopped");
}

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

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Intent myIntent = new Intent(context, service.class);
    context.startService(myIntent);

}

}

XML清单意图

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

XML清单权限

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

0 个答案:

没有答案