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.AsyncTask;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
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;
/*
* 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 PushService2 extends 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 = "192.168.1.71";
// private static final String MQTT_HOST = "192.168.1.18";
private static final String MQTT_HOST = "192.168.1.190";
// the port at which the broker is running.
private static int MQTT_BROKER_PORT_NUM = 1833;
// 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.
public 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.
public static final 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,0,0,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 = "Tokudu";
// Notification id
private static 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, PushService2.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, PushService2.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, PushService2.class);
i.setAction(ACTION_KEEPALIVE);
ctx.startService(i);
}
@Override
public void onCreate() {
super.onCreate();
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() {
unregisterReceiver(mConnectivityChanged);
// 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);
// 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, if the service is already running.
if (mStarted == true) {
Log.w(TAG, "Attempt to start connection that
没有合适的资源?快使用搜索试试~ 我知道了~
MQTTAndroid客户端AndroidPushNotificationsDemo
共55个文件
class:23个
java:7个
xml:5个
5星 · 超过95%的资源 需积分: 9 1.2k 下载量 72 浏览量
2013-07-26
15:26:24
上传
评论 7
收藏 316KB ZIP 举报
温馨提示
MQTTAndroid客户端AndroidPushNotificationsDemo完全可以运行。有问题可留言。
资源推荐
资源详情
资源评论
收起资源包目录
AndroidPushNotificationsDemo.zip (55个子文件)
AndroidPushNotificationsDemo
wmqtt.jar 47KB
.project 831B
project.properties 647B
src
com
tokudu
demo
ConnectionLog.java 2KB
PushActivity.java 2KB
PushService.java 17KB
PushService2.java 18KB
ConnectServerTask.java 15KB
AndroidManifest.xml 1KB
hs_err_pid3120.log 5KB
res
drawable-ldpi
drawable-hdpi
drawable
icon.png 1KB
logo.png 10KB
drawable-mdpi
layout
main.xml 2KB
drawable-xhdpi
values
colors.xml 151B
strings.xml 380B
.settings
org.eclipse.jdt.core.prefs 177B
assets
gen
com
tokudu
demo
BuildConfig.java 157B
R.java 1KB
libs
wmqtt.jar 47KB
hs_err_pid3884.log 5KB
.classpath 433B
.gitignore 12B
README 123B
bin
resources.ap_ 15KB
classes
com
tokudu
demo
R$layout.class 385B
PushService2$1.class 2KB
ConnectServerTask$MQTTConnection.class 6KB
PushService$1.class 2KB
BuildConfig.class 339B
R$drawable.class 419B
R$attr.class 331B
PushService2$ConnectionTask.class 1KB
PushActivity.class 2KB
ConnectServerTask$1.class 2KB
PushService2$MQTTConnection.class 5KB
PushActivity$2.class 1KB
PushService2.class 12KB
R$id.class 558B
ConnectServerTask$ConnectionCallBack.class 285B
ConnectServerTask.class 10KB
PushActivity$1.class 2KB
ConnectionLog.class 2KB
R.class 555B
R$string.class 550B
PushService$MQTTConnection.class 4KB
PushService.class 12KB
R$color.class 428B
AndroidManifest.xml 1KB
dexedLibs
wmqtt-9286df4012176708ef472aca6aefadbe.jar 27KB
annotations-b979e30db1ee24d9ca09e4bf1f51f290.jar 943B
jarlist.cache 119B
res
drawable
icon.png 1KB
logo.png 10KB
PushActivity.apk 56KB
classes.dex 125KB
共 55 条
- 1
Chiclaim
- 粉丝: 2033
- 资源: 56
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页