Search code examples
androidalarmmanager

How to pass constructors through Alarm Manager?


When calling a class with Alarm Manager I get the error saying:

"No method with zero constructors"

Is there a way to pass constructors or an object with AlarmManager or is my only option just adding a method without constructors?

(Without using the Serializable method)

EDIT:

(being called from a Service class) Alarm Manager Code:

public void startCollector(){
        final int LOOP_REQUEST_CODE = 4;
        Intent i = new Intent(getApplicationContext(),DataCollector.class);
        PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(),LOOP_REQUEST_CODE,i,0);
        long firstTime = SystemClock.elapsedRealtime();
        firstTime += 3*1000;
        Log.v("SPAM","Set");

        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 1000, sender);
    }

The called class code:

package com.project.backgroundprocesstest;

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

public class DataCollector extends BroadcastReceiver{
    LocationControl lc = null;
    public DataCollector(){

    }
    public DataCollector(Context context){
        lc = new LocationControl(context);
    }
    @Override
    public void onReceive(Context context,Intent intent){
        collectData(context);
    }
    private void collectData(Context context){
        HttpConnect conn = new HttpConnect();
        try {
            if (lc.getLocation() != null)
                Log.v("SPAM", lc.getLocation());
        }catch (NullPointerException e){

        }
        Log.v("SPAM", "SEND");
    }
}

Goal:

I want to call collectData() from the same DataCollector instance every ~5 mins to collect data and show it in the notification.


Solution

  • Is there a way to pass constructors or an object with AlarmManager

    There is no concept in Java of "pass constructors".

    You use AlarmManager with a PendingIntent. A PendingIntent, in turn, works with an Activity, Service, or BroadcastReceiver. All of those require a zero-argument public constructor, as that is all the Android framework knows how to use to create instances of those classes.

    is my only option just adding a method without constructors?

    There is no concept in Java of methods having constructors.

    You use AlarmManager with a PendingIntent. A PendingIntent, in turn, works with an Activity, Service, or BroadcastReceiver. You need to create an Activity, Service, or BroadcastReceiver for your use with AlarmManager, without constructors, but otherwise with the methods that you need for whichever of those components that you choose to use (e.g., onReceive() for a BroadcastReceiver).

    In your code sample, you may as well move your lc = new LocationControl(context); into onReceive(). Your DataCollector will be used for one call to onReceive(), then discarded, and so you need to create new LocationControl objects each time anyway.

    I want to call collectData() from the same DataCollector instance every ~5 mins to collect data and show it in the notification

    That's not how Android works. Have your DataCollector save its data somewhere (database, SharedPreferences, or other file), reloading it as needed. If you want to have some sort of singleton cache of this information around, that is fine, so long as you are careful to avoid memory leaks. However, it is just a cache; your process can be terminated in between AlarmManager events, and so you cannot rely upon that cache being populated on any given event.