My main problem is "When i click start button but It did not change TextView".
I am trying Service.I use Handler object for calling broadcast then it show textView.But it did not work what problem i can not understand.please help me.
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;
}
}
My layout 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>