androidpn的学习研究(四)androidpn-client客户端几个类说明

本文介绍AndroidPN客户端核心类的功能,包括消息服务管理、网络状态处理等,并详细展示了属性加载过程、SharedPreferences使用方法及通知发送的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 在androidpn的客户端几个重要的类:

                ServiceManager:管理消息服务和加载相关的配置。

                ConnectivityReceiver:处理网络状态的广播。

                NotificationReceiver:处理服务端发送的推送消息。

                NotificationService:后台服务用户响应服务端的消息。需要在AndroidManifest.xml.注册。

                NotificationSettingsActivity:推送信息设置页面。

                PersistentConnectionListener:监控连接关闭和重连事件的监听。

                PhoneStateChangeListener:监听手机状态的事件监听类。

                ReconnectionThread:重连的线程类。

                Notifier:客户端发送通知的类。

                NotificationIQ:消息的数据包。

 

 

ServiceManager中获取属性信息的方法:

Java代码  收藏代码
  1. private Properties loadProperties() {  
  2.     //        InputStream in = null;  
  3.     //        Properties props = null;  
  4.     //        try {  
  5.     //            in = getClass().getResourceAsStream(  
  6.     //                    "/org/androidpn/client/client.properties");  
  7.     //            if (in != null) {  
  8.     //                props = new Properties();  
  9.     //                props.load(in);  
  10.     //            } else {  
  11.     //                Log.e(LOGTAG, "Could not find the properties file.");  
  12.     //            }  
  13.     //        } catch (IOException e) {  
  14.     //            Log.e(LOGTAG, "Could not find the properties file.", e);  
  15.     //        } finally {  
  16.     //            if (in != null)  
  17.     //                try {  
  18.     //                    in.close();  
  19.     //                } catch (Throwable ignore) {  
  20.     //                }  
  21.     //        }  
  22.     //        return props;  
  23.   
  24.     Properties props = new Properties();  
  25.     try {  
  26.         int id = context.getResources().getIdentifier("androidpn""raw",  
  27.                 context.getPackageName());  
  28.         props.load(context.getResources().openRawResource(id));  
  29.     } catch (Exception e) {  
  30.         Log.e(LOGTAG, "Could not find the properties file.", e);  
  31.         // e.printStackTrace();  
  32.     }  
  33.     return props;  
  34. }  

 

 

SharedPreferences的使用:

Java代码  收藏代码
  1. sharedPrefs = context.getSharedPreferences(  
  2.         Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);  
  3. Editor editor = sharedPrefs.edit();  
  4. editor.putString(Constants.API_KEY, apiKey);  
  5. editor.putString(Constants.VERSION, version);  
  6. editor.putString(Constants.XMPP_HOST, xmppHost);  
  7. editor.putInt(Constants.XMPP_PORT, Integer.parseInt(xmppPort));  
  8. editor.putString(Constants.CALLBACK_ACTIVITY_PACKAGE_NAME,  
  9.         callbackActivityPackageName);  
  10. editor.putString(Constants.CALLBACK_ACTIVITY_CLASS_NAME,  
  11.         callbackActivityClassName);  
  12. editor.commit();  

 

获取手机的设备id:

Java代码  收藏代码
  1. TelephonyManager       telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
  2.         // wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
  3.         // connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
  4.   
  5.         // Get deviceId  
  6.         deviceId = telephonyManager.getDeviceId();  

 Notifier中发送通知的方法:

Java代码  收藏代码
  1. // Notification  
  2. Notification notification = new Notification();  
  3. notification.icon = getNotificationIcon();  
  4. notification.defaults = Notification.DEFAULT_LIGHTS;  
  5. if (isNotificationSoundEnabled()) {  
  6.     notification.defaults |= Notification.DEFAULT_SOUND;  
  7. }  
  8. if (isNotificationVibrateEnabled()) {  
  9.     notification.defaults |= Notification.DEFAULT_VIBRATE;  
  10. }  
  11. notification.flags |= Notification.FLAG_AUTO_CANCEL;  
  12. notification.when = System.currentTimeMillis();  
  13. notification.tickerText = message;  
  14.   
  15. //            Intent intent;  
  16. //            if (uri != null  
  17. //                    && uri.length() > 0  
  18. //                    && (uri.startsWith("http:") || uri.startsWith("https:")  
  19. //                            || uri.startsWith("tel:") || uri.startsWith("geo:"))) {  
  20. //                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));  
  21. //            } else {  
  22. //                String callbackActivityPackageName = sharedPrefs.getString(  
  23. //                        Constants.CALLBACK_ACTIVITY_PACKAGE_NAME, "");  
  24. //                String callbackActivityClassName = sharedPrefs.getString(  
  25. //                        Constants.CALLBACK_ACTIVITY_CLASS_NAME, "");  
  26. //                intent = new Intent().setClassName(callbackActivityPackageName,  
  27. //                        callbackActivityClassName);  
  28. //                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  29. //                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
  30. //            }  
  31.   
  32. Intent intent = new Intent(context,  
  33.         NotificationDetailsActivity.class);  
  34. intent.putExtra(Constants.NOTIFICATION_ID, notificationId);  
  35. intent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);  
  36. intent.putExtra(Constants.NOTIFICATION_TITLE, title);  
  37. intent.putExtra(Constants.NOTIFICATION_MESSAGE, message);  
  38. intent.putExtra(Constants.NOTIFICATION_URI, uri);  
  39. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  40. intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);  
  41. intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);  
  42. intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
  43. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  44.   
  45. PendingIntent contentIntent = PendingIntent.getActivity(context, 0,  
  46.         intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  47.   
  48. notification.setLatestEventInfo(context, title, message,  
  49.         contentIntent);  
  50. 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));

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值