0

我有一个观点:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/textLOG"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:ems="10"
        android:inputType="textMultiLine"
        android:scrollbars="vertical" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:text="@string/systemStarted"
        />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity
{

    @Override
    protected void onCreate( Bundle savedInstanceState ) 
    {
        super.onCreate( savedInstanceState );

        setContentView( R.layout.activity_main );
        Globals.getInstance().setParent( this );
    }

    @Override
    public boolean onCreateOptionsMenu( Menu menu ) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate( R.menu.main, menu );
        return true;
    }
}

以及每秒向视图发送文本的服务。

public class CameraService extends Service
{
    private final long   mDelay  = 0;
    private final long   mPeriod = 1000;
    private final String LOGTAG  = "BootDemoService";

    private Timer mTimer;

    private class LogTask extends TimerTask 
    {
        public void run() 
        {
            Log.i( LOGTAG, "scheduled" );
            Globals.getInstance().addToLog( new Date().toString() );
        }
    }

    private LogTask mLogTask;

    @Override
    public IBinder onBind( final Intent intent ) 
    {
        return null;
    }

    @Override
    public void onCreate() 
    {
        super.onCreate();

        Log.i( LOGTAG, "created" );

        mTimer   = new Timer();
        mLogTask = new LogTask();
    }

    @Override
    public void onStart( final Intent intent, final int startId )
    {
        super.onStart( intent, startId );

        Log.i( LOGTAG, "started" );

        mTimer.schedule( mLogTask, mDelay, mPeriod );
    }
}

通过这个单身人士:

public class Globals 
{   
    private Activity parent;

    private static Globals instance = null;

    public static Globals getInstance()
    {
        if( instance == null ) 
        {
            instance = new Globals();
        }

        return instance;
    }


    public Globals()
    {
        super();
    }

    public void setParent( Activity parent ) 
    {
        this.parent = parent;
    }

    public void addToLog( String o )
    {

        if( parent != null )
        {
            TextView text = (EditText)parent.findViewById( R.id.textLOG );

            text.setText( text.getText() + "\n" + o );
        }
    }
}

但文本从未显示在视图中。

  • 如何将文本从服务发送到视图?
4

1 回答 1

3

这不是在serviceand之间进行通信的适当方式activity,最简单的替代方法是使用LocalBroadcast.

service如果和之间的交互activity比较频繁,可以bind到服务中,在服务中设置一个对象,当某个事件发生时listener通知。activity

于 2013-02-24T19:50:43.213 回答