Java 安卓3的服务
我的主要问题是“当我点击开始按钮但它没有改变文本视图时” 我正在尝试服务。我使用处理程序对象调用广播,然后它显示文本视图。但它不工作,我不明白什么问题。请帮助我 MainActivity.javaJava 安卓3的服务,java,android,android-service,android-broadcast,android-handler,Java,Android,Android Service,Android Broadcast,Android Handler,我的主要问题是“当我点击开始按钮但它没有改变文本视图时” 我正在尝试服务。我使用处理程序对象调用广播,然后它显示文本视图。但它不工作,我不明白什么问题。请帮助我 MainActivity.java package com.example.service_ahsanul; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import and
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");
}
}
}
}
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;
}
}
MyServices.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");
}
}
}
}
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 run方法中的语句来调用handleMessage函数。我同意vijay的观点 我看不到您在任何地方注册接收邮件,我也不确定您打算接收什么邮件
为了你的目的,我也会抵制简短的名字。最好为意图使用完全限定的名称。因此,我将使用“com.example.service\u ahsanul.service\u action”而不是“service\u action”您需要在onStart中注册接收器,并在onDestroy中注销它。我认为您的代码不会在暂停时命中,因此您的接收器从未注册(因为您的活动从未被推到后台)。接收器注册在onResume中启动,并在暂停时停止…Bro您从未在暂停或恢复时命中。您不暂停,因此无法继续。什么情况会导致您的活动暂停?