[英]Services for Android 3
我的主要问题是“当我单击开始按钮但它没有更改TextView时 ”。
我正在尝试Service.I使用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>
在您的服务内部,不会从任何地方调用处理程序的handleMessage方法,因此不会执行代码,也不会更新textview内容...
您可以使用handler.postMessage(“ msg”); MyThread运行方法中的语句调用handleMessage函数。
我同意维杰。
我看不到您可能已经注册接收邮件的任何地方,也不确定您打算接收什么邮件。
我也不会出于您的意图而简称。 首选使用意图的全限定名称。 因此,我将使用“ com.example.service_ahsanul.service_action”代替“ service_action”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.