简体   繁体   中英

Class init failed in newInstance call : for IntentService called from alarmManager

I am trying to use IntentService with AlarmManager to Schedule Alarms. I have used BroadCast Reciever that further calls my IntentService here is the code :

Setting Alarm:

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

Here is my BroadCast Reciever class:

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

And Finally I have the below IntentService Class where i am also getting current location of user:

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

}

In Manifest i have registered reciever and service as :

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

But now when the alarm fires on set time it doesn't get to the breakpoint at onReceive of the BroadCast reciever. And i get error for the IntentService. Here is the error log :

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)

尝试向您的服务中添加另一个接受String参数'name'的构造函数,并将其传递给super的构造函数

The problem was in my onConnected method inside IntentService . There was some runtime error in OnConnected method that was causing IntentService to not fire.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM