将文本从服务发送到视图?

时间:2013-02-24 19:21:18

标签: android

我有一个观点:

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

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

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

1 个答案:

答案 0 :(得分:3)

这不是serviceactivity之间沟通的合适方式,最简单的方法是使用LocalBroadcast

如果serviceactivity之间的互动更频繁,您可以bind到服务并在服务中设置listener对象以通知{ {1}}当某些事件发生时。