2010-06-27 4 views
2

Désolé si le titre est un peu déroutant, et si les tags Android n'appartiennent pas vraiment, je pensais les ajouter car c'est pour une application Android. Ce que je veux faire, c'est pouvoir accéder à l'objet neoApi dans la classe Neoseeker, à partir de sa classe interne RunningTimer. Maintenant, dans mon code, vous pouvez voir ce que je penserais à travailler, mais quand je lance mon application, rien ne s'affiche. Rien dans mon TextView, aucun Toast, rien du tout. Comment puis-je remédier à cela? Merci!Accéder à des trucs de niveau classe à partir d'une classe interne

package com.neoseeker.android.app; 

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

import org.json.JSONObject; 

import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Neoseeker extends Activity { 

    NeoAPI neoApi = new NeoAPI(); 
    Timer timer = new Timer(); 

    TextView text; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Set up Timer 
     timer.scheduleAtFixedRate(new RunningTimer(), 0, 1000 * 60 * 3); // Runs every 3 minutes 

     text = (TextView) findViewById(R.id.text); 

    } 

    /** 
    * Causes a status bar notification 
    * @param contentText The text to display to describe the notification 
    */ 
    public void causeNotification(CharSequence contentText) { 
     String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 
     int icon = R.drawable.icon; 
     CharSequence tickerText = "Neoseeker"; 
     long when = System.currentTimeMillis(); 

     Notification notification = new Notification(icon, tickerText, when); 
     notification.defaults |= Notification.DEFAULT_ALL; 
     notification.defaults |= Notification.FLAG_AUTO_CANCEL; 

     Context context = getApplicationContext(); 
     CharSequence contentTitle = "Neoseeker"; 
     Intent notificationIntent = new Intent(); 
     PendingIntent contentIntent = PendingIntent.getActivity(Neoseeker.this, 0, notificationIntent, 0); 

     notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
     int NEOSEEKER_ID = 125468131; // Random ID? 

     mNotificationManager.notify(NEOSEEKER_ID, notification); 
    } 

    class RunningTimer extends TimerTask { 

     boolean sent_notification = false; 
     int unread_pm_count_at_last_check = 0; 
     public void run() { 
      /* 
      int unread = Neoseeker.this.neoApi.getPmUnread(); 
      if (unread > 0) { 
       if (!sent_notification) { 
        Neoseeker.this.causeNotification("You have " + unread + " private message" + ((unread > 1) ? "s." ? ".")); 
       } 
      } 
      */ 
      int unread = Neoseeker.this.neoApi.getPmUnread(); 
      Toast.makeText(Neoseeker.this, "new: " + unread, Toast.LENGTH_SHORT).show(); 
      Neoseeker.this.text.setText("this is the text! " + unread); 
     } 
    } 
} 
+0

Avez-vous essayé de définir un point d'arrêt dans run() pour voir si l'exécution atteint réellement le code? –

+0

Ouais mon code atteint à l'intérieur de run(), donc je sais que ce n'est pas le problème. – Chiggins

Répondre

1

Vous devez exécuter l'appel de modification d'interface utilisateur sur le thread d'interface utilisateur. Ceci est une solution rapide pour votre code (méthode intérieur run() de RunningTimer):

final int unread = Neoseeker.this.neoApi.getPmUnread(); 
Main.this.runOnUiThread(new Runnable() { 

    public void run() { 

     Toast.makeText(Main.this, "new: " + unread, Toast.LENGTH_SHORT).show(); 
     Neoseeker.this.text.setText("this is the text! " + unread); 

    } 

}); 

La meilleure façon de le faire est creux Handler.

+0

Qu'est-ce que c'est exactement? J'ai copié et collé votre solution rapide, et Main a fini par être souligné en rouge lors de l'utilisation d'Eclipse. – Chiggins

+0

Désolé, il devrait être Neoseeker – ognian

+0

Ha, cela fonctionne à merveille. Merci ognian :) – Chiggins

Questions connexes