【问题标题】:Services for Android 3适用于 Android 3 的服务
【发布时间】:2014-05-13 17:01:39
【问题描述】:

我的主要问题是“当我点击开始按钮但它没有改变 TextView”。

我正在尝试服务。我使用 Handler 对象调用广播然后它显示 textView。但是它没有工作我无法理解的问题。请帮助我。

MainActivity.java

package com.example.service_ahsanul;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button btnStart, btnStop;
    TextView txtOutput;
    private ServiceResponseReceiver myreceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnStart = (Button) findViewById(R.id.btnStart);
        btnStop = (Button) findViewById(R.id.btnStop);
        txtOutput = (TextView) findViewById(R.id.txtOutput);

        btnStart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(MainActivity.this, MyServices.class);
                startService(intent);

            }
        });
        btnStop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(MainActivity.this, MyServices.class);
                stopService(intent);

            }

        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Register broadcast port theke
        if (myreceiver == null) {
            myreceiver = new ServiceResponseReceiver();
            IntentFilter filter = new IntentFilter("service_action");
            registerReceiver(myreceiver, filter);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        // unregister broadcast port theke
        if (myreceiver != null) {
            unregisterReceiver(myreceiver);
        }
    }

    class ServiceResponseReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equalsIgnoreCase("service_action")) {
                txtOutput.append("service has finished task\n");
            }

        }


    }

}

MyServices.java

package com.example.service_ahsanul;

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

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class MyServices extends Service {

    private Timer timer;
    private TimerTask task;
    private int counter = 0;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.e("MyService", "onCreate");
    }

    @Override
    @Deprecated
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Log.e("MyServices", "onStart");
        counter = 0;
        handleStart();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Log.e("MyServices", "onStartCommand");
        handleStart();
        return START_STICKY;
    }

    @Override
    public boolean stopService(Intent name) {
        // TODO Auto-generated method stub
        return super.stopService(name);
    }

    private void handleStart() {

        new MyThread().start();
    }

    class MyThread extends Thread {
        @Override
        public void run() {

            try {
                Thread.sleep(1000);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            // Notify the activity broadcast korbe
            sendBroadcast(new Intent("service_action"));
        }
    };

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.e("MyService", "onDestroy");
//      timer.cancel();
//      timer = null;

    }

}

我的布局 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/btnStart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Start" />
    <Button
        android:id="@+id/btnStop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Stop" />
    <TextView
        android:id="@+id/txtOutput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

【问题讨论】:

  • 您需要在 onStart 中注册您的接收器并在 onDestroy 中取消注册。我认为您的代码不会触发 onPause,因此您的接收器永远不会注册(因为您的 Activity 永远不会被推送到后台)。
  • 接收器注册在 onResume 开始并在 onPause 停止....兄弟
  • 你永远不会点击 onPause 或 onResume。您不暂停,因此无法继续。发生了什么导致您的活动暂停?

标签: java android android-service android-broadcast android-handler


【解决方案1】:

我同意 vijay。

我没有看到您注册接收消息的任何地方,我也不确定您打算接收什么消息。

我也拒绝为您的意图使用短名称。首选使用完全限定名称来表示意图。所以我会使用“com.example.service_ahsanul.service_action”而不是“service_action”

【讨论】:

    【解决方案2】:

    在您的服务内部,不会从任何地方调用处理程序的 handleMessage 方法,因此不会执行其代码并且不会更新 textview 内容...

    你可以使用 handler.postMessage("msg"); MyThread run 方法中的语句调用handleMessage函数。

    【讨论】:

      猜你喜欢
      • 2019-08-17
      • 1970-01-01
      • 2012-08-31
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      相关资源
      最近更新 更多