在androidpn的客户端几个重要的类:
ServiceManager:管理消息服务和加载相关的配置。
ConnectivityReceiver:处理网络状态的广播。
NotificationReceiver:处理服务端发送的推送消息。
NotificationService:后台服务用户响应服务端的消息。需要在AndroidManifest.xml.注册。
NotificationSettingsActivity:推送信息设置页面。
PersistentConnectionListener:监控连接关闭和重连事件的监听。
PhoneStateChangeListener:监听手机状态的事件监听类。
ReconnectionThread:重连的线程类。
Notifier:客户端发送通知的类。
NotificationIQ:消息的数据包。
ServiceManager中获取属性信息的方法:
- privatePropertiesloadProperties(){
- //InputStreamin=null;
- //Propertiesprops=null;
- //try{
- //in=getClass().getResourceAsStream(
- //"/org/androidpn/client/client.properties");
- //if(in!=null){
- //props=newProperties();
- //props.load(in);
- //}else{
- //Log.e(LOGTAG,"Couldnotfindthepropertiesfile.");
- //}
- //}catch(IOExceptione){
- //Log.e(LOGTAG,"Couldnotfindthepropertiesfile.",e);
- //}finally{
- //if(in!=null)
- //try{
- //in.close();
- //}catch(Throwableignore){
- //}
- //}
- //returnprops;
- Propertiesprops=newProperties();
- try{
- intid=context.getResources().getIdentifier("androidpn","raw",
- context.getPackageName());
- props.load(context.getResources().openRawResource(id));
- }catch(Exceptione){
- Log.e(LOGTAG,"Couldnotfindthepropertiesfile.",e);
- //e.printStackTrace();
- }
- returnprops;
- }
SharedPreferences的使用:
- sharedPrefs=context.getSharedPreferences(
- Constants.SHARED_PREFERENCE_NAME,Context.MODE_PRIVATE);
- Editoreditor=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:
- TelephonyManagertelephonyManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
- //wifiManager=(WifiManager)getSystemService(Context.WIFI_SERVICE);
- //connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
- //GetdeviceId
- deviceId=telephonyManager.getDeviceId();
Notifier中发送通知的方法:
- //Notification
- Notificationnotification=newNotification();
- 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;
- //Intentintent;
- //if(uri!=null
- //&&uri.length()>0
- //&&(uri.startsWith("http:")||uri.startsWith("https:")
- //||uri.startsWith("tel:")||uri.startsWith("geo:"))){
- //intent=newIntent(Intent.ACTION_VIEW,Uri.parse(uri));
- //}else{
- //StringcallbackActivityPackageName=sharedPrefs.getString(
- //Constants.CALLBACK_ACTIVITY_PACKAGE_NAME,"");
- //StringcallbackActivityClassName=sharedPrefs.getString(
- //Constants.CALLBACK_ACTIVITY_CLASS_NAME,"");
- //intent=newIntent().setClassName(callbackActivityPackageName,
- //callbackActivityClassName);
- //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- //intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
- //}
- Intentintent=newIntent(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);
- PendingIntentcontentIntent=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));