在androidpn的客户端几个重要的类:
ServiceManager:管理消息服务和加载相关的配置。
ConnectivityReceiver:处理网络状态的广播。
NotificationReceiver:处理服务端发送的推送消息。
NotificationService:后台服务用户响应服务端的消息。需要在AndroidManifest.xml.注册。
NotificationSettingsActivity:推送信息设置页面。
PersistentConnectionListener:监控连接关闭和重连事件的监听。
PhoneStateChangeListener:监听手机状态的事件监听类。
ReconnectionThread:重连的线程类。
Notifier:客户端发送通知的类。
NotificationIQ:消息的数据包。
ServiceManager中获取属性信息的方法:
private Properties loadProperties() { // InputStream in = null; // Properties props = null; // try { // in = getClass().getResourceAsStream( // "/org/androidpn/client/client.properties"); // if (in != null) { // props = new Properties(); // props.load(in); // } else { // Log.e(LOGTAG, "Could not find the properties file."); // } // } catch (IOException e) { // Log.e(LOGTAG, "Could not find the properties file.", e); // } finally { // if (in != null) // try { // in.close(); // } catch (Throwable ignore) { // } // } // return props; Properties props = new Properties(); try { int id = context.getResources().getIdentifier("androidpn", "raw", context.getPackageName()); props.load(context.getResources().openRawResource(id)); } catch (Exception e) { Log.e(LOGTAG, "Could not find the properties file.", e); // e.printStackTrace(); } return props; }
SharedPreferences的使用:
sharedPrefs = context.getSharedPreferences( Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); Editor editor = sharedPrefs.edit(); editor.putString(Constants.API_KEY, apiKey); editor.putString(Constants.VERSION, version); editor.putString(Constants.XMPP_HOST, xmppHost); editor.putInt(Constants.XMPP_PORT, Integer.parseInt(xmppPort)); editor.putString(Constants.CALLBACK_ACTIVITY_PACKAGE_NAME, callbackActivityPackageName); editor.putString(Constants.CALLBACK_ACTIVITY_CLASS_NAME, callbackActivityClassName); editor.commit();
获取手机的设备id:
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); // connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); // Get deviceId deviceId = telephonyManager.getDeviceId();
Notifier中发送通知的方法:
// Notification Notification notification = new Notification(); notification.icon = getNotificationIcon(); notification.defaults = Notification.DEFAULT_LIGHTS; if (isNotificationSoundEnabled()) { notification.defaults |= Notification.DEFAULT_SOUND; } if (isNotificationVibrateEnabled()) { notification.defaults |= Notification.DEFAULT_VIBRATE; } notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.when = System.currentTimeMillis(); notification.tickerText = message; // Intent intent; // if (uri != null // && uri.length() > 0 // && (uri.startsWith("http:") || uri.startsWith("https:") // || uri.startsWith("tel:") || uri.startsWith("geo:"))) { // intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); // } else { // String callbackActivityPackageName = sharedPrefs.getString( // Constants.CALLBACK_ACTIVITY_PACKAGE_NAME, ""); // String callbackActivityClassName = sharedPrefs.getString( // Constants.CALLBACK_ACTIVITY_CLASS_NAME, ""); // intent = new Intent().setClassName(callbackActivityPackageName, // callbackActivityClassName); // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // } Intent intent = new Intent(context, NotificationDetailsActivity.class); intent.putExtra(Constants.NOTIFICATION_ID, notificationId); intent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey); intent.putExtra(Constants.NOTIFICATION_TITLE, title); intent.putExtra(Constants.NOTIFICATION_MESSAGE, message); intent.putExtra(Constants.NOTIFICATION_URI, uri); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(context, title, message, contentIntent); notificationManager.notify(random.nextInt(), notification);
androidpn用户名和密码来源:
XmppManager的注册任务(RegisterTask)中run方法:
if (!xmppManager.isRegistered()) {
final String newUsername = newRandomUUID();
final String newPassword = newRandomUUID();
Registration registration = new Registration();
PacketFilter packetFilter = new AndFilter(new PacketIDFilter(
registration.getPacketID()), new PacketTypeFilter(
IQ.class));