我嘗試創建我的第一個服務,當然沒有成功。但這個應用程序成功編譯,並沒有給AndroidMonitor的任何錯誤。 總之,按鈕沒有反應按下。 我加入到Android清單需要行:吐司不在服務
<service android:name=".MyService"></service>
ClientActivity
package com.example.servicetest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ClientActivity extends Activity {
private Button btnStartService;
private Button btnStopService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartService = (Button) findViewById(R.id.btnStartService);
btnStopService = (Button) findViewById(R.id.btnStopService);
initButtonsOnClick();
}
private void initButtonsOnClick() {
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStartService:
startMyService();
break;
case R.id.btnStopService:
stopMyService();
break;
default:
break;
}
}
};
btnStartService.setOnClickListener(listener);
btnStopService.setOnClickListener(listener);
}
private void startMyService() {
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
}
private void stopMyService() {
Intent serviceIntent = new Intent(this, MyService.class);
stopService(serviceIntent);
}
}
爲MyService
package com.example.servicetest;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;
import android.widget.Toast;
public class MyService extends Service {
private Toast toast;
private Timer timer;
private TimerTask timerTask;
private class MyTimerTask extends TimerTask {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Your service is still working", Toast.LENGTH_SHORT).show();
}
}
private void writeToLogs(String message) {
Log.d("HelloServices", message);
}
@Override
public void onCreate() {
super.onCreate();
writeToLogs("Called onCreate() method.");
timer = new Timer();
Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
writeToLogs("Called onStartCommand() methond");
clearTimerSchedule();
initTask();
timer.scheduleAtFixedRate(timerTask, 4 * 1000, 4 * 1000);
Toast.makeText(getApplicationContext(), "Your service has been started", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
private void clearTimerSchedule() {
if(timerTask != null) {
timerTask.cancel();
timer.purge();
}
}
private void initTask() {
timerTask = new MyTimerTask();
}
@Override
public void onDestroy() {
writeToLogs("Called onDestroy() method");
clearTimerSchedule();
Toast.makeText(getApplicationContext(), "Your service has been stopped", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
做'敬酒= Toast.makeText(getApplicationContext() 「」,Toast.LENGTH_SHORT);' –
仍然沒有工作 – philips