package app.manbo;
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.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText vi;
Button btn_reset;
int count = 0;
String str = String.format("%d",count);
Button btnStopService;
Intent intentMyService;
BroadcastReceiver receiver;
boolean flag = true;
Toast toast;
TextView CountText;
String serviceData;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intentMyService = new Intent(this, MyServiceIntent.class);
// 실행되기 원하는 서비스 등록
receiver = new MyMainLocalRecever();
CountText = (TextView) findViewById(R.id.textView01);
btnStopService = (Button) findViewById(R.id.btnStopService);
// 서비스 중지
btn_reset = (Button)findViewById(R.id.btn_reset);
// 서비스 초기화
btn_reset.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
count = 0;
str = String.format("%d", count);
vi.setText(str);
}
});
btnStopService.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (flag) {
btnStopService.setText("Stop !!");
// TODO Auto-generated method stub
try {
IntentFilter mainFilter = new IntentFilter(
"com.androday.test.step");
registerReceiver(receiver, mainFilter);
startService(intentMyService);
// txtMsg.setText("After stoping Service:\n"+service.getClassName());
Toast.makeText(getApplicationContext(), "서비스 시작", 1)
.show();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), e.getMessage(),
1).show();
}
}
else {
btnStopService.setText("Go !!");
// TODO Auto-generated method stub
try {
unregisterReceiver(receiver);
stopService(intentMyService);
Toast.makeText(getApplicationContext(), "서비스 중지", 1)
.show();
// txtMsg.setText("After stoping Service:\n"+service.getClassName());
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), e.getMessage(),
1).show();
}
}
flag = !flag;
}
});
}
class MyMainLocalRecever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
serviceData = intent.getStringExtra("serviceData");
CountText.setText(serviceData);
Toast.makeText(getApplicationContext(), "Walking . . . ", 1).show();
}
}
}
여기서 버튼 리셋이 숫자 카운트한거를 0으로 초기화 시키는건데 어플실행하여 초기화버튼을 누르면 어플이 중지가 됩니다. 어떤점이 잘못되었는지 전문가님들 확인부탁드립니다.