尝试通过服务中的活动中的广播接收字符串,但没有看到任何内容
下面是我的代码。第一段代码用于我的活动。我在启动服务的活动中的按钮上放置了一个单击侦听器。现在,我只想看看我可以从该服务接收字符串,所以这是我尝试执行此操作的代码。我在服务的oncreate中放了一个“hello world”广播来测试。有人能发现这个问题吗?服务代码位于活动代码下方。
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;
}
}
Here's my code below. The first bit of code is for my activity. I put a click listener on a button in that activity that starts my service. Right now, I just want to see that I can recieve strings from that service so here is my code trying to do that. I put a "hello world" broadcast in the oncreate of the service to test. Can anyone spot the issue? The service code is below the activity code.
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于您的实际问题,您正在广播的
Intent
与您在registerReceiver()
中使用的IntentFilter
不匹配。由于您的IntentFilter
表示它正在寻找"mymessage"
Intents
,因此您广播的Intent
需要具有“mymessage”
操作。现在,您的一个Intents
有一个基于当前时间的操作,该操作不太可能被任何东西接收。另一个具有“Hello World”
操作。除此之外:
默认情况下,广播会广播到整个设备。如果您打算向整个设备进行广播,请使用您的应用程序命名的操作字符串(例如,
homeBrewChatter.Calcs.mymessage
)。如果您不打算广播到整个设备,请在广播的Intent
上使用setPackage()
,以将其保留在您的应用程序中。由于
Service
是一个Context
,因此您无需在Service
中使用getApplicationContext()
您似乎没有调用
startTimer()
。在生产代码中,拥有一个主要工作是坐着看着时间流逝的
Service
并不是一个好主意。AlarmManager
是这里更常见的解决方案,因此您的Service
仅当它为用户添加实际价值时才可以在内存中。您启动服务,但永远不会停止它。
With respect to your actual question, the
Intent
that you are broadcasting does not match theIntentFilter
you are using inregisterReceiver()
. Since yourIntentFilter
says it is looking for"mymessage"
Intents
, yourIntent
that you broadcast needs to have the"mymessage"
action. Right now, one of yourIntents
has an action that is based on the current time, which is unlikely to be received by anything. The other has an action of"Hello World"
.Beyond that:
By default, broadcasts are broadcast to the entire device. If you are intending to broadcast to the entire device, please use an action string that is namespaced to your app (e.g.,
homeBrewChatter.Calcs.mymessage
). If you are not intending to broadcast to the entire device, please usesetPackage()
on theIntent
that you broadcast to keep it within your application.Since
Service
is aContext
, you do not need to usegetApplicationContext()
in yourService
for your broadcasts.You do not appear to be calling
startTimer()
.It is infrequently a good idea, in production code, to have a
Service
whose primary job is to sit and watch time tick by.AlarmManager
is a more common solution here, so yourService
can be in memory only when it is adding actual value to the user.You start the service, but never stop it.