从内部类访问类级别的东西

时间:2010-06-27 06:01:12

标签: java android

很抱歉,如果标题有点令人困惑,如果Android标签确实不属于我,我想我会添加它们,因为这是针对Android应用程序的。

我想做的是,能够从内部类RunningTimer访问Neoseeker类中的对象neoApi。现在,在我的代码中,你可以看到我的想法,但是当我运行我的应用程序时,没有任何东西弹出。我的TextView里面没有任何东西,没有Toast,什么都没有。我该如何解决这个问题?谢谢!

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

1 个答案:

答案 0 :(得分:1)

您应该在UI线程上执行UI修改调用。这是对代码的快速修复(在RunningTimer的run()方法中):

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

    }

});

更好的方法是通过Handler

相关问题