我有一个处理程序的服务,必须每5秒在logcat中写入"Hello".但是它没有在logcat上写任何内容......就像服务没有执行一样,我在它上面放了一个断点,调试模式永远不会在断点上停止.
我在我的应用程序的第一个活动中启动了这项服务:
startService(new Intent(GPSLoc.this, MyServiceNotifications.class)); //enciendo el service
Run Code Online (Sandbox Code Playgroud)
我确信代码startService已执行,因为它在启动另一个活动之前被调用,而另一个活动开始.
这是我的服务代码:
public class MyServiceNotifications extends Service {
boolean serviceStopped;
private Handler mHandler;
private Runnable updateRunnable = new Runnable() {
@Override
public void run() {
if (serviceStopped == false)
{
createNotificationIcon();
}
queueRunnable();
}
};
private void queueRunnable() {
// 600000 : cada 10 minutos, comprueba si hay nuevas notificaciones y actualiza la
// notification BAR
mHandler.postDelayed(updateRunnable, 5000);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
serviceStopped = false;
// //////////////////////////////////////MANEJADOR SIMILAR A UN HILO
mHandler = new Handler();
queueRunnable();
// ///////////////////////////////////// FIN MANEJADOR
}
@Override
public void onDestroy() {
serviceStopped = true;
}
@Override
public void onStart(Intent intent, int startid) {
}
public void createNotificationIcon()
{
Log.d("MyServiceNotifications", "Hello");
}
}
Run Code Online (Sandbox Code Playgroud)
Fra*_*nco 177
你有没有申报服务AndroidManifest.xml?
Ser*_*m's 63
非常重要:正确编写名称空间,例如:
<service android:name="com.example.data.synchronization.SynchronizationService"/>
Run Code Online (Sandbox Code Playgroud)
在我AndroidManifest.xml之前它是(错误的):
<service android:name="com.example.data.SynchronizationService"/>
Run Code Online (Sandbox Code Playgroud)
没有服务启动,也没有错误消息!
Ven*_*ddy 16
嗨你写的代码工作正常.在关闭应用程序标记之前,可能会忘记清单文件中的以下代码.
<application>
....
<service android:name=".MyServiceNotifications"/>
</application>
Run Code Online (Sandbox Code Playgroud)
在清单中定义它时,还需要将"enabled"属性设置为"true",如下所示:
<service android:enabled="true" android:name=".MyServiceNotifications" />
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅此链接:http://developer.android.com/guide/topics/manifest/service-element.html
| 归档时间: |
|
| 查看次数: |
44609 次 |
| 最近记录: |