Вот мой код ниже. Первый бит кода для моей деятельности. Я включил прослушиватель щелчков на кнопке в том действии, которое запускает мой сервис. Прямо сейчас я просто хочу увидеть, что я могу получать строки из этого сервиса, поэтому вот мой код, пытающийся это сделать. Я поставил трансляцию «Привет, мир!» На сайте сервиса для тестирования. Кто-нибудь может определить проблему? Код услуги находится под кодом активности.
package homeBrewChatter.Calcs;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import java.util.List;
public class Hop_Timer extends Activity {
private TextView timerOut;
private BroadcastReceiver onBroadcast = new BroadcastReceiver() {
@Override
public void onReceive(Context ctxt, Intent i) {
Hop_Timer.this.timerOut = (TextView)Hop_Timer.this.findViewById(R.id.display_time);
Hop_Timer.this.timerOut.setText("RECIEVED");
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hoptimer);
Button setVars = (Button) findViewById(R.id.add_alarm_button);
setVars.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Hop_Timer.this.timerOut = (TextView)Hop_Timer.this.findViewById(R.id.display_time);
//Hop_Timer.this.timerOut.setText("working so far");
startService(new Intent(Hop_Timer.this, Hop_Timer_Service.class));
}
});
}
public void onResume() {
super.onResume();
registerReceiver(onBroadcast, new IntentFilter("mymessage"));
}
public void onPause() {
super.onPause();
unregisterReceiver(onBroadcast);
}
}
package homeBrewChatter.Calcs;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
public class Hop_Timer_Service extends Service {
private Handler mHandler = new Handler();
private int length_minutes;
private boolean timer_running;
private long startTime = 0L;
public String currentTime = "";
@Override
public void onCreate() {
length_minutes = 0;
timer_running = false;
getApplicationContext().sendBroadcast(new Intent("Hello World"));
}
public void setTime(int mins) {
length_minutes = mins;
}
public void startTimer() {
startTime = System.currentTimeMillis();
mHandler.removeCallbacks(mUpdateTimerTask);
mHandler.postDelayed(mUpdateTimerTask, 100);
}
private Runnable mUpdateTimerTask = new Runnable() {
public void run() {
final long start = startTime;
long millis = SystemClock.uptimeMillis() - start;
int seconds = (int) (millis/1000);
int minutes = seconds /60;
seconds = seconds %60;
if(seconds < 10) {
currentTime = "" + minutes + ":0" + seconds;
} else {
currentTime = "" + minutes + ":" + seconds;
}
getApplicationContext().sendBroadcast(new Intent(currentTime));
mHandler.postAtTime(this, start + (((minutes * 60) + seconds + 1) * 1000));
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}