package com.tokudu.demo;
import java.io.IOException;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.util.Log;
import com.ibm.mqtt.IMqttClient;
import com.ibm.mqtt.MqttClient;
import com.ibm.mqtt.MqttException;
import com.ibm.mqtt.MqttPersistence;
import com.ibm.mqtt.MqttPersistenceException;
import com.ibm.mqtt.MqttSimpleCallback;
import com.lee.android.rtx.R;
import com.lee.android.rtx.activity.Chat;
import com.lee.android.rtx.bean.myclient;
import com.lee.android.rtx.service.UserService;
/*
* PushService that does all of the work.
* Most of the logic is borrowed from KeepAliveService.
* http://code.google.com/p/android-random/source/browse/trunk/TestKeepAlive/src/org/devtcg/demo/keepalive/KeepAliveService.java?r=219
*/
public class PushService extends Service
{
private UserService service ;
// this is the log tag
public static final String TAG = "DemoPushService";
// the IP address, where your MQTT broker is running.
private static final String MQTT_HOST = "10.147.49.167";
// the port at which the broker is running.
private static int MQTT_BROKER_PORT_NUM = 1883;
// Let's not use the MQTT persistence.
private static MqttPersistence MQTT_PERSISTENCE = null;
// We don't need to remember any state between the connections, so we use a clean start.
private static boolean MQTT_CLEAN_START = true;
// Let's set the internal keep alive for MQTT to 15 mins. I haven't tested this value much. It could probably be increased.
private static short MQTT_KEEP_ALIVE = 60 * 15;
// Set quality of services to 0 (at most once delivery), since we don't want push notifications
// arrive more than once. However, this means that some messages might get lost (delivery is not guaranteed)
private static int[] MQTT_QUALITIES_OF_SERVICE = { 0 } ;
private static int MQTT_QUALITY_OF_SERVICE = 0;
// The broker should not retain any messages.
private static boolean MQTT_RETAINED_PUBLISH = false;
// MQTT client ID, which is given the broker. In this example, I also use this for the topic header.
// You can use this to run push notifications for multiple apps with one MQTT broker.
public static String MQTT_CLIENT_ID = "tokudu";
// These are the actions for the service (name are descriptive enough)
private static final String ACTION_START = MQTT_CLIENT_ID + ".START";
private static final String ACTION_STOP = MQTT_CLIENT_ID + ".STOP";
private static final String ACTION_KEEPALIVE = MQTT_CLIENT_ID + ".KEEP_ALIVE";
private static final String ACTION_RECONNECT = MQTT_CLIENT_ID + ".RECONNECT";
// Connection log for the push service. Good for debugging.
private ConnectionLog mLog;
// Connectivity manager to determining, when the phone loses connection
private ConnectivityManager mConnMan;
// Notification manager to displaying arrived push notifications
private NotificationManager mNotifMan;
// Whether or not the service has been started.
private boolean mStarted;
// This the application level keep-alive interval, that is used by the AlarmManager
// to keep the connection active, even when the device goes to sleep.
private static final long KEEP_ALIVE_INTERVAL = 1000 * 60 * 28;
// Retry intervals, when the connection is lost.
private static final long INITIAL_RETRY_INTERVAL = 1000 * 10;
private static final long MAXIMUM_RETRY_INTERVAL = 1000 * 60 * 30;
// Preferences instance
private SharedPreferences mPrefs;
// We store in the preferences, whether or not the service has been started
public static final String PREF_STARTED = "isStarted";
// We also store the deviceID (target)
public static final String PREF_DEVICE_ID = "deviceID";
// We store the last retry interval
public static final String PREF_RETRY = "retryInterval";
// Notification title
public static String NOTIF_TITLE = "RTX�連";
// Notification id
private static final int NOTIF_CONNECTED = 0;
// This is the instance of an MQTT connection.
private MQTTConnection mConnection;
private long mStartTime;
// Static method to start the service
public static void actionStart(Context ctx) {
Intent i = new Intent(ctx, PushService.class);
i.setAction(ACTION_START);
ctx.startService(i);
}
// Static method to stop the service
public static void actionStop(Context ctx) {
Intent i = new Intent(ctx, PushService.class);
i.setAction(ACTION_STOP);
ctx.startService(i);
}
// Static method to send a keep alive message
public static void actionPing(Context ctx) {
Intent i = new Intent(ctx, PushService.class);
i.setAction(ACTION_KEEPALIVE);
ctx.startService(i);
}
@Override
public void onCreate() {
super.onCreate();
service = new UserService(this);
log("Creating service");
mStartTime = System.currentTimeMillis();
try {
mLog = new ConnectionLog();
Log.i(TAG, "Opened log at " + mLog.getPath());
} catch (IOException e) {
Log.e(TAG, "Failed to open log", e);
}
// Get instances of preferences, connectivity manager and notification manager
mPrefs = getSharedPreferences(TAG, MODE_PRIVATE);
mConnMan = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
mNotifMan = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
/* If our process was reaped by the system for any reason we need
* to restore our state with merely a call to onCreate. We record
* the last "started" value and restore it here if necessary. */
handleCrashedService();
}
// This method does any necessary clean-up need in case the server has been destroyed by the system
// and then restarted
private void handleCrashedService() {
if (wasStarted() == true) {
log("Handling crashed service...");
// stop the keep alives
stopKeepAlives();
// Do a clean start
start();
}
}
@Override
public void onDestroy() {
log("Service destroyed (started=" + mStarted + ")");
// Stop the services, if it has been started
if (mStarted == true) {
stop();
}
try {
if (mLog != null)
mLog.close();
} catch (IOException e) {}
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
log("Service started with intent=" + intent);
service = new UserService(getApplicationContext());
// Do an appropriate action based on the intent.
if (intent.getAction().equals(ACTION_STOP) == true) {
stop();
stopSelf();
} else if (intent.getAction().equals(ACTION_START) == true) {
start();
} else if (intent.getAction().equals(ACTION_KEEPALIVE) == true) {
keepAlive();
} else if (intent.getAction().equals(ACTION_RECONNECT) == true) {
if (isNetworkAvailable()) {
reconnectIfNecessary();
}
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
// log helper function
private void log(String message) {
log(message, null);
}
private void log(String message, Throwable e) {
if (e != null) {
Log.e(TAG, message, e);
} else {
Log.i(TAG, message);
}
if (mLog != null)
{
try {
mLog.println(message);
} catch (IOException ex) {}
}
}
// Reads whether or not the service has been started from the preferences
private boolean wasStarted() {
return mPrefs.getBoolean(PREF_STARTED, false);
}
// Sets whether or not the services has been started in the preferences.
private void setStarted(boolean started) {
mPrefs.edit().putBoolean(PREF_STARTED, started).commit();
mStarted = started;
}
private synchronized void start() {
log("Starting service...");
// Do nothing,
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
腾讯通(rtx)android客户端 (101个子文件)
resources.ap_ 176KB
RTX.apk 229KB
nan.bmp 2KB
nv.bmp 2KB
proguard.cfg 1KB
PushService.class 12KB
Chat.class 7KB
TreeView.class 7KB
RTXActivity.class 6KB
RTXActivity$1.class 6KB
PushService$MQTTConnection.class 5KB
myclient.class 5KB
RTXActivity$TreeViewAdapter.class 5KB
LoginActivity$1.class 4KB
TreeView$TreeViewAdapter.class 4KB
UserService.class 4KB
Chat$3.class 4KB
Chat$2.class 2KB
ConnectionLog.class 2KB
PDFOutlineElement.class 2KB
User.class 2KB
PushActivity.class 2KB
Chat$1.class 2KB
ChatRecord.class 2KB
PushActivity$1.class 2KB
PushService$1.class 2KB
Chat$2$1.class 2KB
LoginActivity.class 1KB
DBHelper.class 1KB
R$id.class 1KB
PushActivity$2.class 1KB
R$drawable.class 1KB
DepartMent.class 995B
Chat$2$2.class 910B
RTXActivity$TreeViewAdapter$ViewHolder.class 792B
TreeView$TreeViewAdapter$ViewHolder.class 771B
R$string.class 655B
R.class 636B
R$layout.class 547B
R$color.class 440B
R$style.class 400B
MyListView.class 390B
R$attr.class 343B
.classpath 343B
classes.dex 107KB
outline_list_expand.ico 2KB
outline_list_collapse.ico 2KB
outline_list_collapse.ico 2KB
outline_list_expand.ico 2KB
ren.ico 2KB
checkbox.ico 1KB
checkbox_empty.ico 1KB
wmqtt.jar 47KB
PushService.java 18KB
RTXActivity.java 11KB
TreeView.java 9KB
Chat.java 7KB
myclient.java 5KB
R.java 4KB
LoginActivity.java 4KB
UserService.java 3KB
PushActivity.java 2KB
PDFOutlineElement.java 2KB
ConnectionLog.java 2KB
User.java 1KB
ChatRecord.java 1KB
DBHelper.java 809B
DepartMent.java 504B
MyListView.java 265B
default_bg.png 111KB
logo.png 10KB
icon.png 6KB
icon.png 6KB
middle.png 6KB
user_group.png 3KB
ren_bak.png 2KB
icon.png 2KB
rtx.png 2KB
child_image.png 2KB
btn_browser2.png 2KB
btn_browser.png 1KB
title.png 1KB
close.png 716B
max.png 546B
min.png 442B
fgx.png 165B
divider.png 157B
org.eclipse.core.resources.prefs 126B
.project 839B
default.properties 362B
login.xml 3KB
mqtt.xml 2KB
main.xml 2KB
AndroidManifest.xml 2KB
outline.xml 1KB
chatrecord.xml 1011B
chat.xml 719B
strings.xml 513B
checkbox_selector.xml 443B
styles.xml 331B
共 101 条
- 1
- 2
ff379514238
- 粉丝: 2
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Editor下载非常好用的工具
- tensorflow-rocm-2.12.1.570-cp38-cp38-manylinux2014-x86-64.whl
- tensorflow-rocm-2.12.1.570-cp311-cp311-manylinux2014-x86-64.whl
- tensorflow-gpu-2.7.1-cp38-cp38-manylinux2010-x86-64.whl
- 路劲
- 基于matlab实现的拉普拉斯金字塔分解 做毕业设计的可以参考,小波变换以及MGA的初级参考.rar
- 基于matlab实现的拉普拉斯金字塔分解的图像融合源程序.rar
- 基于matlab实现的里面介绍的是使用禁忌搜索求解vrp,只要修改下数据就可以使用,用的是MATLAB写的.rar
- 基于matlab实现的邻接矩阵和级联失效模拟.rar
- 基于matlab实现的论文“连续相空间转换中的级联故障”代码.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页