Androidpn学习与使用3

昨天想把androidpn_client整合到自己的客户端,失败了。搞了半天没成功,应该不是简单地把类,配置文件,jar包引进来这么简单。还是先分析分析源代码吧。

DemoAppActivity是项目的主界面,onCreate方法如下:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d("DemoAppActivity", "onCreate()...");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Settings
        Button okButton = (Button) findViewById(R.id.btn_settings);
        okButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ServiceManager.viewNotificationSettings(DemoAppActivity.this);
            }
        });

        // Start the service
        ServiceManager serviceManager = new ServiceManager(this);//主要是写入一些配置信息
        serviceManager.setNotificationIcon(R.drawable.notification);//设置推送信息的icon
        serviceManager.startService();//开启服务
    }
这里红色部分是主要需要注意的,这里就是我们自己的项目中主要用到的代码,但是具体里面怎么实现的貌似大有讲究啊,我们要一个个看过来。

 ServiceManager serviceManager = new ServiceManager(this);//主要是写入一些配置信息
public ServiceManager(Context context) {
		this.context = context;

		if (context instanceof Activity) {
			Log.i(LOGTAG, "Callback Activity...");// LOGTAG其实就是Androidpn_ServiceManager
			Activity callbackActivity = (Activity) context;// 主界面Activity
			callbackActivityPackageName = callbackActivity.getPackageName();// 主界面所在的包
			Log.i(LOGTAG, "callbackActivityPackageName..."
					+ callbackActivityPackageName);
			callbackActivityClassName = callbackActivity.getClass().getName();// 主界面的类名
			Log.i(LOGTAG, "callbackActivityClassName..."
					+ callbackActivityClassName);
		}

		props = loadProperties();// 得到anroidpn.properties中的配置文件
		apiKey = props.getProperty("apiKey", "");
		xmppHost = props.getProperty("xmppHost", "127.0.0.1");
		xmppPort = props.getProperty("xmppPort", "5222");
		Log.i(LOGTAG, "apiKey=" + apiKey);
		Log.i(LOGTAG, "xmppHost=" + xmppHost);
		Log.i(LOGTAG, "xmppPort=" + xmppPort);

		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();// 提交编辑器,其实是往sharedPrefs中写入一些配置数据

		// Log.i(LOGTAG, "sharedPrefs=" + sharedPrefs.toString());
	}

这个是ServiceManager构造方法源代码,

sharedPrefs = context.getSharedPreferences(
				Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);

一开始看不懂这句话,去网上百度了一下,又实际操作了一下,原来SharePreferences类是一个轻量级的配置类,contect.getSharedPreferences方法里面的两个参数,第一个是一个文件名,比如说 "client_preferences",这样的话,手机里面的data/data/你的项目包名 这个目录下会有一个share_prefer文件夹,里面会自动生成 

client_preferences.xml文件。这个文件里面刚才put进去的值,都会成为配置信息。至于第二个参数,大概是别的应用程序能不能访问的权限问题


这是官方API给的四个常量,仅供参考。

 serviceManager.setNotificationIcon(R.drawable.notification);//设置推送信息的icon
这句话是设置提示框的icon的,其实也是写到配置文件里

  serviceManager.startService();//开启服务
最后,也是最重要的就是这句话,之前的两句话都是一些配置信息,现在是开启服务了。

看看startService方法,源代码:

public void startService() {// 开启一个线程,用来跑intent
		Thread serviceThread = new Thread(new Runnable() {
			@Override
			public void run() {
				Log.i("开启线程。。", "线程运行。。。。。");
				Intent intent = NotificationService.getIntent();
				context.startService(intent);
			}
		});
		serviceThread.start();
	}
NotificationService.getIntent():

public static Intent getIntent() {
		Log.d(LOGTAG, "getIntent()...");
		return new Intent(SERVICE_NAME);
	}

无非是通过这个方法开启一个Service,至于问什么放在线程里面运行,有个群里的大哥解释的很好:

恩,现在启动这个服务了,最后要看的就是这个服务的onCreate和onStart方法了,这才是主要学习的。

public NotificationService() {
		notificationReceiver = new NotificationReceiver();
		connectivityReceiver = new ConnectivityReceiver(this);
		phoneStateListener = new PhoneStateChangeListener(this);
		executorService = Executors.newSingleThreadExecutor();
		taskSubmitter = new TaskSubmitter(this);
		taskTracker = new TaskTracker(this);
	}

这个是NotificationService的构造方法,里面这么多属性对象需要初始化。下篇文章开始一一分析。

对了new Intent(SERVICE_NAME);  

这个东西是Intent的一个构造方法,SERVICE_NAME在我这是"org.androidpn.client.NotificationService",

这个必须在AndroidMenifect.xml中配过才能用。

这里

<service android:enabled="true"
			android:name="org.androidpn.client.NotificationService"
			android:label="NotificationService">
			<intent-filter>
				<action android:name="org.androidpn.client.NotificationService" />
			</intent-filter>
</service>
这个action就是用来识别这个的。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值