newInstance调用中的类init失败:对于从alarmManager调用的IntentService

时间:2014-06-26 06:13:00

标签: android broadcastreceiver alarmmanager intentservice

我正在尝试使用带有AlarmManager的IntentService来安排警报。我使用BroadCast Reciever进一步调用我的IntentService这里是代码:

设置闹钟:

public static Integer createScheduledAlarm(Calendar calendar,
        int intervalSeconds, Context context) {

    AlarmManager alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);

    int id = (int) System.currentTimeMillis();

    Intent intent = new Intent(context,
            com.stressfree.alerts.TimeAlarm.class);


    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    if (intervalSeconds > 0) {
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), intervalSeconds * 1000,
                pendingIntent);
    }   
    return id;
}

这是我的BroadCast Reciever课程:

public class TimeAlarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    //Bundle bundle = intent.getExtras();
    Intent taskService = new Intent(context, TaskService.class);
    //taskService.putExtras(bundle);
    context.startService(taskService);
    }
}

最后我有下面的IntentService类,我也得到了用户的当前位置:

public class TaskService extends IntentService implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener{


public TaskService() {
    super("TaskService");
}

private LocationClient locationclient;
private Location mCurrentLocation;
private Context mContext;



@Override
protected void onHandleIntent(Intent intent) {

    Log.e("Service", "Inside onHandleIntent");
    mContext = this;
    int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (resp == ConnectionResult.SUCCESS) {
        locationclient = new LocationClient(this, this, this);
        locationclient.connect();
    }

}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onConnected(Bundle arg0) {
    mCurrentLocation = locationclient.getLastLocation();
    //Do some stuff
}

@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

}

在Manifest中,我已将注册人和服务注册为:

        <receiver android:name=".TimeAlarm" />
        <service android:name=".services.TaskService" >

但现在当闹钟在设定的时间触发时,它无法到达BroadCast接收器的 onReceive 处的断点。我得到了IntentService的错误。这是错误日志:

06-26 11:22:20.068: W/dalvikvm(4070): VFY: register1 v2 type 17, wanted 18
06-26 11:22:20.068: W/dalvikvm(4070): VFY:  rejecting opcode 0x2e at 0x005c
06-26 11:22:20.073: W/dalvikvm(4070): VFY:  rejected Lcom/stressfree/services/TaskService;.onConnected (Landroid/os/Bundle;)V
06-26 11:22:20.073: W/dalvikvm(4070): Verifier rejected class Lcom/stressfree/services/TaskService;
06-26 11:22:20.073: W/dalvikvm(4070): Class init failed in newInstance call (Lcom/stressfree/services/TaskService;)
06-26 11:22:20.073: D/AndroidRuntime(4070): Shutting down VM
06-26 11:22:20.078: W/dalvikvm(4070): threadid=1: thread exiting with uncaught exception (group=0x40c321f8)


06-26 11:22:20.088: E/AndroidRuntime(4070): FATAL EXCEPTION: main
06-26 11:22:20.088: E/AndroidRuntime(4070): java.lang.VerifyError: com/stressfree/services/TaskService
06-26 11:22:20.088: E/AndroidRuntime(4070):     at java.lang.Class.newInstanceImpl(Native Method)
06-26 11:22:20.088: E/AndroidRuntime(4070):     at java.lang.Class.newInstance(Class.java:1319)
06-26 11:22:20.088: E/AndroidRuntime(4070):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2246)
06-26 11:22:20.088: E/AndroidRuntime(4070):     at android.app.ActivityThread.access$1600(ActivityThread.java:127)
06-26 11:22:20.088: E/AndroidRuntime(4070):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1213)
06-26 11:22:20.088: E/AndroidRuntime(4070):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 11:22:20.088: E/AndroidRuntime(4070):     at android.os.Looper.loop(Looper.java:137)
06-26 11:22:20.088: E/AndroidRuntime(4070):     at android.app.ActivityThread.main(ActivityThread.java:4507)
06-26 11:22:20.088: E/AndroidRuntime(4070):     at java.lang.reflect.Method.invokeNative(Native Method)
06-26 11:22:20.088: E/AndroidRuntime(4070):     at java.lang.reflect.Method.invoke(Method.java:511)
06-26 11:22:20.088: E/AndroidRuntime(4070):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-26 11:22:20.088: E/AndroidRuntime(4070):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-26 11:22:20.088: E/AndroidRuntime(4070):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

尝试向您的服务添加另一个构造函数,该构造函数接受String param&#39; name&#39;并将其传递给super

的构造函数

答案 1 :(得分:0)

问题出在 IntentService 中的 onConnected 方法中。 OnConnected方法中存在一些导致IntentService无法触发的运行时错误。