为什么我的服务没有运行? (通过显示Toast消息来判断。)
我正在尝试在后台创建一项服务,以便我可以运行一个循环,每 x 分钟请求一个页面。这是清单中的我的服务:
<service android:name=".webToSMS" android:enabled="true" />
这是我正在启动的服务(在主要活动中):
Intent intent = new Intent(this, webToSMS.class);
startService(intent);
最后,这是我的服务类:
public class webToSMS extends IntentService {
public webToSMS() {
super("webToSMS");
}
@Override
protected void onHandleIntent(Intent intent) {
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
我正在遵循 Android 的指南,这就是它告诉我要做的事情。我期待的是弹出一个祝酒词“Hello toast!”当该服务运行时。最终,当这个工作正常时,我将放置一个循环,每 x 分钟请求一个页面。
I am trying to make a service in the background so I can run a loop that requests a page every x minutes. This is my service in the manifest:
<service android:name=".webToSMS" android:enabled="true" />
And here is my service being started (in the main activity):
Intent intent = new Intent(this, webToSMS.class);
startService(intent);
And finally, this is my service class:
public class webToSMS extends IntentService {
public webToSMS() {
super("webToSMS");
}
@Override
protected void onHandleIntent(Intent intent) {
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
I was following the guide at Android and this is what it told me to do. What I am expecting is a toast to pop up saying "Hello toast!" when this service is run. Eventually when this works I will put a loop which will request a page every x minutes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的服务正在运行,只是没有显示 toast,因为您不在 UI 线程上。
如果你想看吐司试试这个
Your service is running, it's just not displaying the toast because you are not on the UI thread.
If you want to see a toast try this instead