Android 后台Service : 向服务器发送心跳包
来源:互联网 发布:手机吉他软件下载 编辑:程序博客网 时间:2024/05/25 21:36
带有心跳包的简单Demo,包括服务器 http://git.oschina.net/fengcunhan/SocketTest/blob/master/SocketTestService/src/com/mina/MinaServer.java?dir=0&filepath=SocketTestService%2Fsrc%2Fcom%2Fmina%2FMinaServer.java&oid=599789369632a6a2e8a31297c0cf10f85605a82c&sha=debc947b8ffa9433064384eacc01065b73aaa7b1
Android 关于休眠的几个坑点
http://blog.csdn.net/dreamer0924/article/details/7705144
[java]
public class HeartbeatService extends Service implements Runnable
{
private Thread mThread;
public int count = 0;
private boolean isTip = true;
private static String mRestMsg;
private static String KEY_REST_MSG = "KEY_REST_MSG";
@Override
public void run()
{
while (true)
{
try
{
if (count > 1)
{
Log.i("@qi", "offline");
count = 1;
if (isTip)
{
//判断应用是否在运行
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(3);
for (RunningTaskInfo info : list)
{
if (info.topActivity.getPackageName().equals("org.yhn.demo"))
{
//通知应用,显示提示“连接不到服务器”
Intent intent = new Intent("org.yhn.demo");
intent.putExtra("msg", true);
sendBroadcast(intent);
break;
}
}
isTip = false;
}
}
if (mRestMsg != "" && mRestMsg != null)
{
//向服务器发送心跳包
sendHeartbeatPackage(mRestMsg);
count += 1;
}
Thread.sleep(1000 * 3);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
private void sendHeartbeatPackage(String msg)
{
HttpGet httpGet = new HttpGet(msg);
DefaultHttpClient httpClient = new DefaultHttpClient();
// 发送请求
HttpResponse httpResponse = null;
try
{
httpResponse = httpClient.execute(httpGet);
}
catch (Exception e)
{
e.printStackTrace();
}
if (httpResponse == null)
{
return;
}
// 处理返回结果
final int responseCode = httpResponse.getStatusLine().getStatusCode();
if (responseCode == HttpStatus.SC_OK)
{
//只要服务器有回应就OK
count = 0;
isTip = true;
}
else
{
Log.i("@qi", "responseCode " + responseCode);
}
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
super.onCreate();
}
@Override
public void onDestroy()
{
super.onDestroy();
}
public void onStart(Intent intent, int startId)
{
Log.i("@qi", "service onStart");
//从本地读取服务器的URL,如果没有就用传进来的URL
mRestMsg = getRestMsg();
if (mRestMsg == null || mRestMsg == "")
{
mRestMsg = intent.getExtras().getString("url");
}
setRestMsg(mRestMsg);
mThread = new Thread(this);
mThread.start();
count = 0;
super.onStart(intent, startId);
}
public String getRestMsg()
{
SharedPreferences prefer = getSharedPreferences("settings.data", Context.MODE_PRIVATE);
Log.i("@qi", "getRestMsg() " + prefer.getString(KEY_REST_MSG, ""));
return prefer.getString(KEY_REST_MSG, "");
}
public void setRestMsg(String restMsg)
{
SharedPreferences prefer = getSharedPreferences("settings.data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefer.edit();
editor.putString(KEY_REST_MSG, restMsg);
editor.commit();
}
}
public class HeartbeatService extends Service implements Runnable
{
private ThreadmThread;
public intcount= 0;
private booleanisTip= true;
private static StringmRestMsg;
private static StringKEY_REST_MSG= "KEY_REST_MSG";
@Override
public void run()
{
while (true)
{
try
{
if (count > 1)
{
Log.i("@qi", "offline");
count = 1;
if (isTip)
{
//判断应用是否在运行
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(3);
for (RunningTaskInfo info : list)
{
if (info.topActivity.getPackageName().equals("org.yhn.demo"))
{
//通知应用,显示提示“连接不到服务器”
Intent intent = new Intent("org.yhn.demo");
intent.putExtra("msg", true);
sendBroadcast(intent);
break;
}
}
isTip = false;
}
}
if (mRestMsg != "" && mRestMsg != null)
{
//向服务器发送心跳包
sendHeartbeatPackage(mRestMsg);
count += 1;
}
Thread.sleep(1000 * 3);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
private void sendHeartbeatPackage(String msg)
{
HttpGet httpGet = new HttpGet(msg);
DefaultHttpClient httpClient = new DefaultHttpClient();
// 发送请求
HttpResponse httpResponse = null;
try
{
httpResponse = httpClient.execute(httpGet);
}
catch (Exception e)
{
e.printStackTrace();
}
if (httpResponse == null)
{
return;
}
// 处理返回结果
final int responseCode = httpResponse.getStatusLine().getStatusCode();
if (responseCode == HttpStatus.SC_OK)
{
//只要服务器有回应就OK
count = 0;
isTip = true;
}
else
{
Log.i("@qi", "responseCode " + responseCode);
}
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
super.onCreate();
}
@Override
public void onDestroy()
{
super.onDestroy();
}
public void onStart(Intent intent, int startId)
{
Log.i("@qi", "service onStart");
//从本地读取服务器的URL,如果没有就用传进来的URL
mRestMsg = getRestMsg();
if (mRestMsg == null || mRestMsg == "")
{
mRestMsg = intent.getExtras().getString("url");
}
setRestMsg(mRestMsg);
mThread = new Thread(this);
mThread.start();
count = 0;
super.onStart(intent, startId);
}
public String getRestMsg()
{
SharedPreferences prefer = getSharedPreferences("settings.data", Context.MODE_PRIVATE);
Log.i("@qi", "getRestMsg() " + prefer.getString(KEY_REST_MSG, ""));
return prefer.getString(KEY_REST_MSG, "");
}
public void setRestMsg(String restMsg)
{
SharedPreferences prefer = getSharedPreferences("settings.data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefer.edit();
editor.putString(KEY_REST_MSG, restMsg);
editor.commit();
}
}
启动Service:
[java]
Intent serviceIntent = new Intent("HeartbeatService");
serviceIntent.putExtra("url",url);
startService(serviceIntent);
Intent serviceIntent = new Intent("HeartbeatService");
serviceIntent.putExtra("url",url);
startService(serviceIntent);
最后别忘了注册Server和GET_TASKS
[html]
<service
android:name=".demo.HeartbeatService"
android:label="QServer"
android:persistent="true" >
<intent-filter>
<action android:name="HeartbeatService" />
</intent-filter>
</service>
<service
android:name=".demo.HeartbeatService"
android:label="QServer"
android:persistent="true" >
<intent-filter>
<action android:name="HeartbeatService" />
</intent-filter>
</service>[html] view plaincopyprint?
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.GET_TASKS" />
运行效果:
0 0
- Android 后台Service : 向服务器发送心跳包
- Android 后台Service : 向服务器发送心跳包
- Android 后台Service : 向服务器发送心跳包
- Android 后台Service : 向服务器发送心跳包
- Android 后台Service : 向服务器发送心跳包
- Android向服务器发送心跳服务
- Android-向服务器发送图片
- Android service 心跳检测
- swoole_client定时发送心跳包
- IOS 后台定位并向服务器发送当前位置
- 【转】IOS 后台定位并向服务器发送当前位置
- android 心跳包
- android通过httpPost向服务器发送请求
- android通过httpPost向服务器发送请求 .
- android通过httpPost向服务器发送请求
- mina心跳包:客户端发送心跳包,服务端响应心跳包
- 【Android】Android心跳包详解
- 判断Socket连接失效,发送心跳包
- Preparing Olympiad
- Linux 压缩与解压缩相关
- php本地文件上传到远程服务器
- 线程间通讯和等待唤醒机制
- java内置数据结构--Map典型应用
- Android 后台Service : 向服务器发送心跳包
- OJ刷题---简单密码破解
- 内核中的中断函数request_irq()
- 冒泡排序
- hash 链地址法+ 数组模拟邻接表实现
- 黑马程序员_@protocol 协议
- Divisibility by Eight
- Aspose Cells如何进行数据验证(后附下载地址)
- 自定义控件--下拉刷新