0

我做了一项服务,他每 5 秒在屏幕上放一个 TAG(我想这就是它的名称)。当我启动时,它需要将 TAG 放在屏幕上,但他说应用程序崩溃了。为什么?代码:Android清单:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <receiver android:name="com.YuvalFatal.MyBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <service android:enabled="true" android:name="com.YuvalFatal.MyService"/>

广播接收器:

  package com.YuvalFatal.ineedhelp;

import java.util.Timer;
import java.util.TimerTask;

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

public class MyBroadcastreceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context arg0, Intent arg1) {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        public void run() {
            Intent startServiceIntent = new Intent(arg0, MyService.class);
            arg0.startService(startServiceIntent);
        }
    }, 0, 5000);
}
}  

意向服务:

package com.YuvalFatal.ineedhelp;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;


public class MyService extends IntentService {

private static final String TAG = "com.YuvalFatal.ineedhelp";

public MyService(String name) {
    super(name);
    // TODO Auto-generated constructor stub
}

@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    Log.i(TAG, "Intent Service started");
}

}
4

1 回答 1

3

我认为(是的,我是魔术师并且有很好的直觉:) 你的 Service 构造函数应该是默认的:

public class MyService extends IntentService {
    ...

    public MyService() { // Default constructor! Without params!
        super("MyService"); // Or another string
    }

...
}

其他代码看起来正常

于 2013-02-06T22:27:15.147 回答