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

本文深入探讨了AndroidPN客户端的重要类及其作用,包括ServiceManager、ConnectivityReceiver、NotificationReceiver等,详细阐述了它们在网络、推送消息处理及服务管理方面的功能,并介绍了如何使用SharedPreferences进行配置,获取手机设备ID,以及在Notifier类中发送通知的方法。

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

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

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

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

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

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

NotificationSettingsActivity:推送信息设置页面。

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

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

ReconnectionThread:重连的线程类。

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

NotificationIQ:消息的数据包。

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

Java代码收藏代码
  1. privatePropertiesloadProperties(){
  2. //InputStreamin=null;
  3. //Propertiesprops=null;
  4. //try{
  5. //in=getClass().getResourceAsStream(
  6. //"/org/androidpn/client/client.properties");
  7. //if(in!=null){
  8. //props=newProperties();
  9. //props.load(in);
  10. //}else{
  11. //Log.e(LOGTAG,"Couldnotfindthepropertiesfile.");
  12. //}
  13. //}catch(IOExceptione){
  14. //Log.e(LOGTAG,"Couldnotfindthepropertiesfile.",e);
  15. //}finally{
  16. //if(in!=null)
  17. //try{
  18. //in.close();
  19. //}catch(Throwableignore){
  20. //}
  21. //}
  22. //returnprops;
  23. Propertiesprops=newProperties();
  24. try{
  25. intid=context.getResources().getIdentifier("androidpn","raw",
  26. context.getPackageName());
  27. props.load(context.getResources().openRawResource(id));
  28. }catch(Exceptione){
  29. Log.e(LOGTAG,"Couldnotfindthepropertiesfile.",e);
  30. //e.printStackTrace();
  31. }
  32. returnprops;
  33. }

SharedPreferences的使用:

Java代码收藏代码
  1. sharedPrefs=context.getSharedPreferences(
  2. Constants.SHARED_PREFERENCE_NAME,Context.MODE_PRIVATE);
  3. Editoreditor=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. TelephonyManagertelephonyManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  2. //wifiManager=(WifiManager)getSystemService(Context.WIFI_SERVICE);
  3. //connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
  4. //GetdeviceId
  5. deviceId=telephonyManager.getDeviceId();

Notifier中发送通知的方法:

Java代码收藏代码
  1. //Notification
  2. Notificationnotification=newNotification();
  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. //Intentintent;
  15. //if(uri!=null
  16. //&&uri.length()>0
  17. //&&(uri.startsWith("http:")||uri.startsWith("https:")
  18. //||uri.startsWith("tel:")||uri.startsWith("geo:"))){
  19. //intent=newIntent(Intent.ACTION_VIEW,Uri.parse(uri));
  20. //}else{
  21. //StringcallbackActivityPackageName=sharedPrefs.getString(
  22. //Constants.CALLBACK_ACTIVITY_PACKAGE_NAME,"");
  23. //StringcallbackActivityClassName=sharedPrefs.getString(
  24. //Constants.CALLBACK_ACTIVITY_CLASS_NAME,"");
  25. //intent=newIntent().setClassName(callbackActivityPackageName,
  26. //callbackActivityClassName);
  27. //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  28. //intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  29. //}
  30. Intentintent=newIntent(context,
  31. NotificationDetailsActivity.class);
  32. intent.putExtra(Constants.NOTIFICATION_ID,notificationId);
  33. intent.putExtra(Constants.NOTIFICATION_API_KEY,apiKey);
  34. intent.putExtra(Constants.NOTIFICATION_TITLE,title);
  35. intent.putExtra(Constants.NOTIFICATION_MESSAGE,message);
  36. intent.putExtra(Constants.NOTIFICATION_URI,uri);
  37. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  38. intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
  39. intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  40. intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  41. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  42. PendingIntentcontentIntent=PendingIntent.getActivity(context,0,
  43. intent,PendingIntent.FLAG_UPDATE_CURRENT);
  44. notification.setLatestEventInfo(context,title,message,
  45. contentIntent);
  46. 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));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值