我希望在進入手機主菜單,與服務器連接以及如果收到消息顯示定製烤麪包之後,服務工作。我做了大部分,但我只能在烤麪包片上顯示文字。 我想用圖像製作自定義的烤麪包,我發現很多解決方案如何在活動中做到這一點,但它不適用於服務。安卓定製烤麪包服務
有人能告訴我該更改哪些代碼才能正常工作?
public class MyService extends Service {
private Toast toast;
private Timer timer;
private TimerTask timerTask;
private class MyTimerTask extends TimerTask {
@Override
public void run() {
showToast();
}
}
private void showToast() {
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.toast, null);
ImageView image = (ImageView)
layout.findViewById(R.id.image);
image.setImageResource(R.drawable.truck);
TextView textView = (TextView)
layout.findViewById(R.id.text);
textView.setText("Some toast message");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
@Override
public void onCreate() {
super.onCreate();
timer = new Timer();
toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
clearTimerSchedule();
initTask();
timer.scheduleAtFixedRate(timerTask, 4 * 1000, 4 * 1000);
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() {
clearTimerSchedule();
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
我想你不能自定義敬酒。無論如何,我會很高興被證明是錯誤的! :) – LittleSweetSeas
請參閱http://developer.android.com/design/patterns/confirming-acknowledging.html以確認您在此處正確使用敬酒。 – adamp
@adamp我只需要通知用戶,他不必確認他看到了,但他也可以確認,我只需要顯示信息 – akuzma