Java 试图在活动中通过广播接收来自服务的字符串,但什么也看不到
下面是我的代码。第一段代码用于我的活动。我在启动我的服务的活动中的按钮上放置了一个单击侦听器。现在,我只想看到我可以从那个服务接收字符串,所以这里是我的代码。我在要测试的服务的oncreate中放置了一个“hello world”广播。有人能发现这个问题吗?服务代码位于活动代码下方Java 试图在活动中通过广播接收来自服务的字符串,但什么也看不到,java,android,service,android-activity,broadcast,Java,Android,Service,Android Activity,Broadcast,下面是我的代码。第一段代码用于我的活动。我在启动我的服务的活动中的按钮上放置了一个单击侦听器。现在,我只想看到我可以从那个服务接收字符串,所以这里是我的代码。我在要测试的服务的oncreate中放置了一个“hello world”广播。有人能发现这个问题吗?服务代码位于活动代码下方 package homeBrewChatter.Calcs; import android.app.Activity; import android.content.BroadcastReceiver; impor
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;
}
}
package homeBrewChatter.Calcs;
导入android.app.Activity;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.content.SharedReferences;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.TextView;
导入android.widget.EditText;
导入android.widget.Button;
导入java.util.List;
公共类跃点计时器扩展活动{
私有文本视图时间路由;
private BroadcastReceiver onBroadcast=新的BroadcastReceiver(){
@凌驾
接收时公共无效(上下文ctxt,意图i){
Hop_Timer.this.timerOut=(TextView)Hop_Timer.this.findviewbyd(R.id.display_time);
Hop_Timer.this.timerOut.setText(“已接收”);
}
};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.hoptimer);
按钮setVars=(按钮)findViewById(R.id.add\u alarm\u按钮);
setVars.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//Hop_Timer.this.timerOut=(TextView)Hop_Timer.this.findviewbyd(R.id.display_time);
//Hop_Timer.this.timerOut.setText(“工作到目前为止”);
startService(新意图(Hop_Timer.this,Hop_Timer_Service.class));
}
});
}
恢复时公开作废(){
super.onResume();
registerReceiver(onBroadcast,新意向过滤器(“mymessage”);
}
公共无效暂停(){
super.onPause();
未注册接收器(onBroadcast);
}
}
包装homeBrewChatter.Calcs;
导入android.app.Service;
导入android.content.Intent;
导入android.os.Handler;
导入android.os.IBinder;
导入android.os.SystemClock;
公共类跃点计时器服务扩展服务{
私有处理程序mHandler=新处理程序();
私人整数长度_分钟;
私有布尔定时器_运行;
专用长启动时间=0L;
公共字符串currentTime=“”;
@凌驾
public void onCreate(){
长度_分钟=0;
计时器运行=错误;
getApplicationContext().sendBroadcast(新意图(“Hello World”);
}
公共无效设置时间(整数分钟){
长度\分钟=分钟;
}
公共无效startTimer(){
startTime=System.currentTimeMillis();
mHandler.removeCallbacks(mUpdateTimerTask);
mHandler.postDelayed(mUpdateTimerTask,100);
}
private Runnable mUpdateTimerTask=new Runnable(){
公开募捐{
最终长时间启动=开始时间;
long millis=SystemClock.uptimeMillis()-开始;
整数秒=(整数)(毫秒/1000);
整数分钟=秒/60;
秒=秒%60;
如果(秒<10){
currentTime=“”+分钟+”:0“+秒;
}否则{
currentTime=”“+分钟+”:“+秒;
}
getApplicationContext().sendBroadcast(新意图(当前时间));
mHandler.posttime(这个,开始+((分*60)+秒+1)*1000));
}
};
@凌驾
公共IBinder onBind(意向){
//TODO自动生成的方法存根
返回null;
}
}
关于您的实际问题,您正在广播的意图
与您在registerReceiver()中使用的意图过滤器
不匹配。由于您的IntentFilter
表示正在查找“mymessage”
意图
,因此您广播的意图
需要执行“mymessage”
操作。现在,您的一个意图
有一个基于当前时间的操作,任何人都不可能收到该操作。另一个有一个动作“Hello World”
除此之外:
- 默认情况下,广播将广播到整个设备。如果您打算向整个设备广播,请使用与应用程序同名的操作字符串(例如,
homeBrewChatter.Calcs.mymessage
)。如果您不打算向整个设备广播,请在您广播的Intent
上使用setPackage()
,将其保留在应用程序中
- 由于
服务
是一个上下文
,您不需要在服务
中为您的广播使用getApplicationContext()
- 您似乎没有调用
startTimer()
- 在生产代码中,让
服务
的主要工作是坐着看时间流逝,这通常不是一个好主意AlarmManager
在这里是一种更常见的解决方案,因此您的服务
只有在向用户添加实际值时才能存储在内存中
- 你启动服务,但从不停止
您是否在AndroidManifest.xml
中设置了接收器?我没有。所以,对于Android来说,除了活动和服务之外,我什么都没意识到。现在我要检查一下。哇,我想我还需要一些东西,以便在我的活动中启动服务。感谢所有这些,我仍然在学习并使用这个项目来自学。我会仔细检查一切的