(混合移动开发)使用cordova-broadcaster插件监听手机屏幕开关广播
最近在开发部门的一个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;@Overridepublic 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 backgroundBundle extras = getIntent().getExtras();if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {moveTaskToBack(true);}// Set by in config.xmlloadUrl(launchUrl);}//从这里往下都是要增加的代码class StateChangeReceiver extends BroadcastReceiver{private String action = null;@Overridepublic 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);}}}@Overridepublic void onDestroy() {super.onDestroy();//取消注册unregisterReceiver(stateChangeReceiver);}
}
增加权限
在 platforms\android\AndroidManifest.xml内增加
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
