最近在开发部门的一个APP,需要有消息推送功能,前端用轮询,一开始用了cordova-plugin-background-mode插件使其可以后台运行,但是用这个插件太耗电了,也没找到解决方法。后来想到可以在手机屏幕解锁时进行查询,又了解到这种监听属于广播,于是就用到了今天要讲的cordova-broadcaster广播插件。
cordova-broadcaster插件官网
怎么添加cordova插件这里就不教学了…
js里的代码
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
if (window.broadcaster) {
console.log(window.broadcaster);
var listener = function( e ) {
//接收到广播信息
//console.log( "didShow received! userInfo: " + JSON.stringify(e) );
}
window.broadcaster.addEventListener( "didShow", listener);
}else {
console.log('windown.broadcaster not available');
}
}
这里要修改 \platforms\android\src\com\包名\项目名\MainActivity.java中的代码,也就是要注册广播。(默认的cordova项目中,此文件是不需要修改的)
接着有一些类要自己引入,比如其中用到的LocalBroadcastManager类,需要引入
import android.support.v4.content.LocalBroadcastManager;
详细代码
package com.mycompany.tt;
import android.os.Bundle;
import org.apache.cordova.*;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.support.v4.content.LocalBroadcastManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends CordovaActivity
{
//下面三行是要增加的代码
private LocalBroadcastManager lbm;
private IntentFilter intentFilter;
private StateChangeReceiver stateChangeReceiver;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//下面7行时要增加的代码
lbm=LocalBroadcastManager.getInstance(this);
intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
intentFilter.addAction(Intent.ACTION_USER_PRESENT);
stateChangeReceiver = new StateChangeReceiver();
registerReceiver(stateChangeReceiver, intentFilter);
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
}
//从这里往下都是要增加的代码
class StateChangeReceiver extends BroadcastReceiver{
private String action = null;
@Override
public void onReceive(Context context, Intent intent) {
action = intent.getAction();
//这里是之前的例子,可以得到网络变化
//ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(Intent.ACTION_SCREEN_ON.equals(action)){
//Toast.makeText(context, "开屏", Toast.LENGTH_SHORT).show();
Intent intents = new Intent("didShow");
Bundle b = new Bundle();
b.putString( "data", "开屏" );
intents.putExtras( b);
lbm.sendBroadcastSync(intents);
}else if(Intent.ACTION_SCREEN_OFF.equals(action)){
//Toast.makeText(context, "锁屏", Toast.LENGTH_SHORT).show();
Intent intents = new Intent("didShow");
Bundle b = new Bundle();
b.putString( "data", "锁屏" );
intents.putExtras( b);
lbm.sendBroadcastSync(intents);
}else if (Intent.ACTION_USER_PRESENT.equals(action)){
//Toast.makeText(context, "解锁", Toast.LENGTH_SHORT).show();
Intent intents = new Intent("didShow");
Bundle b = new Bundle();
b.putString( "data", "解锁" );
intents.putExtras( b);
lbm.sendBroadcastSync(intents);
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
//取消注册
unregisterReceiver(stateChangeReceiver);
}
}
增加权限
在 platforms\android\AndroidManifest.xml内增加
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />