我开发的应用程序与Android工作室 我有一个服务,连接一个网页,并将其读取 如果网页包含“更新”,然后程序创建一个敬酒信息。服务每3秒钟完成一次。 它的工作不错,但20至25分钟后,服务停止工作 我怎样才能解决这个问题Android工作室我的服务在一段时间后停止工作
0
A
回答
0
在破坏你的服务:
@Override
public void onDestroy() {
super.onDestroy();
Intent broadcastIntent = new Intent(".RestartService");
sendBroadcast(broadcastIntent);
}
在你的清单中添加一个接收器:
<receiver
android:name=".ServiceReceiver"
android:enabled="true"
android:exported="true"
android:label="RestartServiceWhenStopped">
<intent-filter>
<action android:name=".RestartService"/>
</intent-filter>
</receiver>
然后,使用以下代码创建名为ServiceReceiver.java的Java文件:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, BackgroundService.class));
}
}
** BackgroundService是您的服务类的名称。此代码将帮助您在由OS或甚至用户关闭服务时自动启动服务。
0
在你的清单中添加一个接收器:
<receiver android:name="com.example.admin.apptech_nation.service.AlarmManagerBroadcastReceiver"/>
在您的MainActivity你需要setRepiting Brodcast
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent myIntent = new Intent(mContext, AlarmManagerBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 60); // first time
long frequency = 60 * 50; // in ms
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), frequency, pendingIntent);
}
在您BroadcastReciver
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver{
Context Mcontext;
@Override
public void onReceive(Context context, Intent intent) {
Mcontext = context;
}
相关问题
- 1. mssql_connect停止工作一段时间后
- 2. node.js服务器停止工作一段时间后winows
- 3. 服务在某段时间后停止工作。需要继续工作
- 4. 支持Https的Localhost HttpListener - 在一段时间后停止工作
- 5. TCP通信器在一段时间后停止工作
- 6. SetWindowsHook在一段时间后停止工作
- 7. freeradius在一段时间后停止工作
- 8. MySQL事件在一段时间后停止工作
- 9. Hammer.js在一段时间后停止工作
- 10. SignalR在一段时间后停止工作
- 11. Perl脚本在一段时间后停止工作
- 12. gnatpp在一段时间后停止工作
- 13. 片段不工作在android工作室
- 14. Windows服务停止工作
- 15. PostgreSQL服务停止工作
- 16. 使用解析的Android推送在一段时间后停止工作
- 17. Android的工作室:MONITOR.EXE已停止工作
- 18. BroadcastReceiver工作一段时间,然后停止
- 19. 媒体播放器一段时间后停止工作
- 20. 地理栅栏工作,但一段时间后停止触发
- 21. Servlet在一些点击或时间后停止在Tomcat服务器上工作
- 22. Android呼叫状态侦听程序在一段时间后停止工作
- 23. Android服务在一段时间后停止运行请帮助
- 24. 服务器端WCF(.svc)服务在异常后停止工作
- 25. Locale在heroku的rails上停止工作了一段时间
- 26. Android AudioManager.setSpeakerphoneOn在睡眠后停止工作
- 27. Android工作室 - 从片段
- 28. Arduino在一段时间后不工作
- 29. Android工作室http服务器
- 30. 包装如果长时间不工作在Android工作室
它看起来不错,但我怎么会连接它到我的服务 – Hahayahahay
如果你想更新任何用户界面,那么你可以在onReceive方法更新它将C所有的每2-3秒其他明智的你可以启动onReceive的Intent Service。意图服务完成您的工作,并在您的工作完成时自行停止。 – Dilip