android心跳包作用,Android 后台Service : 向服务器发送心跳包

[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 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 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

]

android:name=".demo.HeartbeatService"

android:label="QServer"

android:persistent="true" >

android:name=".demo.HeartbeatService"

android:label="QServer"

android:persistent="true" >

[html] view plaincopyprint?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值