0

Can't get over it, no matter how much I search.

I have AlarmManager, where I want to start AsyncTask after every X seconds.

I do have another file, class (Alarm.class) where I have onReceive. Whatever I put inside it, will get activated perfectly.

Now I have problem. I have AsyncTask whats in my main thread/class (Onime). It does many tasks, including downloading some content.

Now problem is, how can I start that AsyncTask from another class. Sadly static didnt work, because asyc included some stuff what doesn't allow static. Is it possible via some kind of service

public class Alarm extends BroadcastReceiver 
{    
     @Override
     public void onReceive(Context context, Intent intent) 
     {   
         // Put here YOUR code.
         Log.d("alarminfo", "Alarm works!"); 


     }

 public void SetAlarm(Context context)
 {
     AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
     Intent i = new Intent(context, Alarm.class);
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
     Calendar time = Calendar.getInstance();
     time.setTimeInMillis(System.currentTimeMillis());
     time.add(Calendar.SECOND, 30);
     am.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), 1000*60*1, pi); // Millisec * Second * Minute
     Log.d("alarminfo", "Set alarm!"); 
 }

 public void CancelAlarm(Context context)
 {
     Log.d("alarminfo", "End alarm!"); 
     Intent intent = new Intent(context, Alarm.class);
     PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
     alarmManager.cancel(sender);
 }
}

Thats Alarm Class.

Here is Async (Its in Onime.class (different file):

class animeUpdator extends AsyncTask<ArrayList<Anime>, Integer, String> {

        class Connection implements Runnable {

            public void run () {
                    try {
                        URL url = new URL("http://xxx.com");
                        URLConnection urlConnection = url.openConnection();
                        BufferedReader bufferedReader = 
                            new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                        String line;
                        count = 1;
                        anime_list.clear();
                        while ((line = bufferedReader.readLine()) != null) {
                            parsingAnime(line);
                            count += 1;
                        }

                        bufferedReader.close();

                        MessageToMainThread(UPDATE_MAIN_GUI);

                    } catch(Exception e) {
                        Log.d("readInfo", e.toString()); 
                    }
                }

        }    
Elven
  • 310
  • 1
  • 3
  • 11

1 Answers1

0

If you need to create a instance of a inner class, you go from its outter class:

Onime.animeUpdator.class.getConstructors()[0].newInstance(new Onime());

Note, Java Class names should start with capital. ex. AnimeUpdator;

More information, see this post and this.
Or as comment pointed out:

public class animeUpdator extends AsyncTask<ArrayList<Anime>, Integer, String> {
   public animeUpdator(){
          ...
   }

}

Community
  • 1
  • 1
wtsang02
  • 18,603
  • 10
  • 49
  • 67
  • The thing is... animeUpdator is in "Onime" class, what is different file :/... (mainActivity) – Elven Dec 17 '12 at 00:53
  • @newest update: No enclosing instance of type Onime is accessible. Must qualify the allocation with an enclosing instance of type Onime (Eg. x.new A() where x is instance of Onime. - thats problem what I got before too. – Elven Dec 17 '12 at 01:01
  • At least it doesnt show error in coding, but throws me exception: 12-17 03:18:18.829: E/AndroidRuntime(647): java.lang.RuntimeException: Unable to start receiver xx.xx.xx.Alarm: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 - and I think it might be already inside AsyncTask problem? – Elven Dec 17 '12 at 01:20
  • try to add a constructor. as I edited. And creating a instance of Asynctask from another class isnt' a good idea to begin with. – wtsang02 Dec 17 '12 at 01:34
  • Thanks, it kinda works :). I do get nullpointer, but I think cause might be this time really something else. Anyway I will look into it myself. Thank you! – Elven Dec 17 '12 at 02:12