功能介绍
实现了点击扫一扫跳转到微信扫一扫,支付宝扫一扫。
点击搜索地名,跳转百度地图,并语言播报
其中专享福利与附近车位分别是横向RecyclerView和竖向滑动RecyclerView
点击附近车位的任意子项可以直接根据子项上的地址直接导航(前提必须安装百度地图)
效果图如下:
需要在清单文件声明如下权限:
<!-- 这个权限用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 这个权限用于访问GPS定位 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 --> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 访问网络,网络定位需要上网 --> <uses-permission android:name="android.permission.INTERNET" /> <!-- 允许挂载和反挂载文件系统可移动存储 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- 允许程序读取底层系统日志文件 --> <uses-permission android:name="android.permission.READ_LOGS" /> <!-- 允许访问振动设备 --> <uses-permission android:name="android.permission.VIBRATE" /> <!-- 允许使用PowerManager的 WakeLocks保持进程在休眠时从屏幕消失 --> <uses-permission android:name="android.permission.WAKE_LOCK" /> <!-- 允许程序读取或写入系统设置 --> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <!-- android 9.0上使用前台服务,需要添加权限 --> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <!-- 用于读取手机当前的状态 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 读取缓存数据 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- 获取模拟定位信息 --> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" /> <!-- 获取应用启动次数权限 --> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <!-- 语音权限 --> <uses-permission android:name="android.permission.PERMISSIONS_STORAGE" /> <!-- 手机定位信息,用来为语义等功能提供定位,提供更精准的服务 --> <!-- 如需使用人脸识别,还要添加:摄相头权限,拍照需要用到 --> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.Camera" /> <uses-feature android:name="android.hardware.Camera.autofocus" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <!-- GUI自动填充验证码功能(非必须) --> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
代码详解
XML布局代码如下:
<?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/Function_goback" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginLeft="@dimen/XXSmall" android:layout_marginTop="@dimen/XXSmall" android:scaleType="fitCenter" android:src="@drawable/goback" /> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@drawable/information1" android:orientation="vertical" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="280dp" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="@dimen/XXSmall" android:background="@drawable/searchbar" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="2dp" android:text="株洲市" /> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:scaleType="fitCenter" android:src="@drawable/dropdown" /> <ImageButton android:id="@+id/Search" android:layout_width="18dp" android:layout_height="20dp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:background="#00000000" android:scaleType="fitCenter" android:src="@drawable/search" /> <EditText android:id="@+id/SearchPlace" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="0dp" android:hint="输入目的地寻找停车位" android:textSize="12sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/Scan" android:layout_width="23dp" android:layout_height="30dp" android:layout_marginLeft="@dimen/XSmall" android:layout_marginTop="@dimen/XSmall" android:background="#00000000" android:scaleType="fitCenter" android:src="@drawable/scan" /> </LinearLayout> </LinearLayout> </LinearLayout> <!--功能模块--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:orientation="horizontal"> <!--图标--> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="4dp" android:scaleType="fitCenter" android:src="@drawable/totalcar"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="车位掌柜" android:textSize="10sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="4dp" android:scaleType="fitCenter" android:src="@drawable/money1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="停车缴费" android:textSize="10sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="4dp" android:scaleType="fitCenter" android:src="@drawable/cards1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="月卡续费" android:textSize="10sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="4dp" android:scaleType="fitCenter" android:src="@drawable/submoney" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="优惠专区" android:textSize="10sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="4dp" android:scaleType="fitCenter" android:src="@drawable/welfare" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="新人福利" android:textSize="10sp" /> </LinearLayout> </LinearLayout> <!--第二模块--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:orientation="horizontal"> <!--图标--> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="4dp" android:scaleType="fitCenter" android:src="@drawable/signin1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="积分签到" android:textSize="10sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="4dp" android:scaleType="fitCenter" android:src="@drawable/people123" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="省钱小蜜" android:textSize="10sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="4dp" android:scaleType="fitCenter" android:src="@drawable/zhangdan" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="我的账单" android:textSize="10sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="4dp" android:scaleType="fitCenter" android:src="@drawable/pay" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="无感支付" android:textSize="10sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="4dp" android:scaleType="fitCenter" android:src="@drawable/fapiao" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="电子发票" android:textSize="10sp" /> </LinearLayout> </LinearLayout> <!--登陆小模块--> <LinearLayout android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:background="@drawable/littlelogin" android:orientation="horizontal"> <TextView android:id="@+id/LoginStatusText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:text="登陆“布道者”智能停车,停车缴费更便捷" android:textColor="#ffffff" android:textSize="10sp" /> <Button android:id="@+id/IntentLogin" android:layout_width="90dp" android:layout_height="30dp" android:layout_marginLeft="30dp" android:layout_marginTop="10dp" android:background="@drawable/loginbutton" android:text="立即登陆" android:textColor="#436EEE" /> </LinearLayout> <!--专享福利--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!--板块1--> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal"> <View android:layout_width="3dp" android:layout_height="20dp" android:layout_marginLeft="10dp" android:background="#40E0D0" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="专享福利" android:textColor="#000" /> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/HorizontalRecyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/XSmall"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/XSmall" android:orientation="horizontal"> <View android:layout_width="3dp" android:layout_height="20dp" android:layout_marginLeft="10dp" android:background="#40E0D0" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="附近车位" android:textColor="#000" /> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/MyRecyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:descendantFocusability="blocksDescendants" /> </LinearLayout> <com.google.android.material.navigation.NavigationView android:id="@+id/InformationManage" android:layout_width="match_parent" android:layout_height="match_parent" app:headerLayout="@layout/information_head_item" app:menu="@menu/setting_menu" android:layout_gravity="left" /> </androidx.drawerlayout.widget.DrawerLayout>
百度地图参数配置
/*******************初始化百度地图各种参数*******************/ private void initLocation() { LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); /**可选,设置定位模式,默认高精度LocationMode.Hight_Accuracy:高精度; * LocationMode. Battery_Saving:低功耗;LocationMode. Device_Sensors:仅使用设备;*/ option.setCoorType("bd09ll"); /**可选,设置返回经纬度坐标类型,默认gcj02gcj02:国测局坐标;bd09ll:百度经纬度坐标;bd09:百度墨卡托坐标; 海外地区定位,无需设置坐标类型,统一返回wgs84类型坐标*/ option.setScanSpan(3000); /**可选,设置发起定位请求的间隔,int类型,单位ms如果设置为0,则代表单次定位,即仅定位一次,默认为0如果设置非0,需设置1000ms以上才有效*/ option.setOpenGps(true); /**可选,设置是否使用gps,默认false使用高精度和仅用设备两种定位模式的,参数必须设置为true*/ option.setLocationNotify(true); /**可选,设置是否当GPS有效时按照1S/1次频率输出GPS结果,默认false*/ option.setIgnoreKillProcess(false); /**定位SDK内部是一个service,并放到了独立进程。设置是否在stop的时候杀死这个进程,默认(建议)不杀死,即setIgnoreKillProcess(true)*/ option.SetIgnoreCacheException(false); /**可选,设置是否收集Crash信息,默认收集,即参数为false*/ option.setIsNeedAltitude(true);/**设置海拔高度*/ option.setWifiCacheTimeOut(5 * 60 * 1000); /**可选,7.2版本新增能力如果设置了该接口,首次启动定位时,会先判断当前WiFi是否超出有效期,若超出有效期,会先重新扫描WiFi,然后定位*/ option.setEnableSimulateGps(false); /**可选,设置是否需要过滤GPS仿真结果,默认需要,即参数为false*/ option.setIsNeedAddress(true); /**可选,设置是否需要地址信息,默认不需要*/ client.setLocOption(option); /**mLocationClient为第二步初始化过的LocationClient对象需将配置好的LocationClientOption对象,通过setLocOption方法传递给LocationClient对象使用*/ }
判断是否安装了百度地图,然后进行跳转搜索
/****************判断百度地图是否安装***************/ public static boolean isBaiduMapInstalled(){ return isInstallPackage(BAIDUMAPPACKAGE); } private static boolean isInstallPackage(String packageName) { return new File("/data/data/" + packageName).exists(); }
跳转以及语言播报
Search.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //根据搜索栏搜索的地点,然后根据自身的经纬度,进行查询 String EndAddress = SearchPlace.getText().toString(); String SpeakContent = "本次导航从当前位置前往"+EndAddress; String Zeng = "警告警告检测到周围有曾大屁出现快疏散人群她要放屁了她要放屁了她要放屁了重要的事情说三遍"; if (TTS != null && !TTS.isSpeaking()) { TTS.setPitch(0.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规 if (EndAddress.equals( "曾大屁" )) { TTS.speak(Zeng,TextToSpeech.QUEUE_FLUSH, null); }else { TTS.speak( SpeakContent, TextToSpeech.QUEUE_FLUSH, null ); } }else { Toast.makeText( Function.this, "语音助手错误", Toast.LENGTH_LONG ).show(); } if (isBaiduMapInstalled()) { Navigation(EndAddress); } else { // 未安装 Toast.makeText( Function.this, "您尚未安装百度地图", Toast.LENGTH_LONG ).show(); } } } ); }
语言播报类初始化
/****************语音助手监听事件***************/ class InitTTS implements TextToSpeech.OnInitListener{ @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { int result = TTS.setLanguage( Locale.CHINA); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { Toast.makeText( Function.this, "数据丢失或不支持", Toast.LENGTH_SHORT).show(); } } } }
接下来介绍跳转到微信扫一扫和支付宝扫一扫
首先判断本地是否安装了两个软件,然后进行跳转
/****************监测是否安装了微信***************/ public static boolean CheckWeiXinInstalled(Context context) { final PackageManager packageManager = context.getPackageManager();// 获取packagemanager List<PackageInfo> pinfo = packageManager.getInstalledPackages( 0 );// 获取所有已安装程序的包信息 if (pinfo != null) { for (int i = 0; i < pinfo.size(); i++) { String pn = pinfo.get( i ).packageName; if (pn.equals( "com.tencent.mm" )) { return true; } } } return false; } /****************监测支付宝是否安装***************/ public static boolean CheckAliPayInstalled(Context context) { Uri uri = Uri.parse("alipays://platformapi/startApp"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); ComponentName componentName = intent.resolveActivity(context.getPackageManager()); return componentName != null; }
/****************跳转到微信扫一扫***************/ private void toWeiXinScan(Context context){ try { Intent intent = Function.this.getPackageManager().getLaunchIntentForPackage( "com.tencent.mm" ); intent.putExtra( "LauncherUI.From.Scaner.Shortcut", true ); startActivity( intent ); } catch (Exception e) { Toast.makeText( Function.this, "没有安装微信", Toast.LENGTH_SHORT ).show(); } } /****************跳转到支付宝扫一扫***************/ public static void toAliPayScan(Context context) { try { Uri uri = Uri.parse("alipayqr://platformapi/startapp?saId=10000007"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); context.startActivity(intent); } catch (Exception e) { Toast.makeText(context, "打开失败,请检查是否安装了支付宝", Toast.LENGTH_SHORT).show(); } }
整体代码如下:
package com.example.Imformation; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.core.content.FileProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.viewpager.widget.ViewPager; import android.Manifest; import android.app.Activity; import android.content.ComponentName; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.speech.tts.TextToSpeech; import android.speech.tts.UtteranceProgressListener; import android.view.Gravity; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.PopupWindow; import android.widget.SearchView; import android.widget.TextView; import android.widget.Toast; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult; import com.example.Adapter.MyPagerAdapter; import com.example.Adapter.MyRecyclerViewHorizontal; import com.example.Adapter.MyRecyclerViewTools; import com.example.Adapter.RecyclerViewHorizontalAdapter; import com.example.AdvanceBooking.Booking; import com.example.Litepal.Account; import com.example.LoginPackage.Login; import com.example.LoginPackage.RegisterInformation; import com.example.Order.OrderInterface; import com.example.SharedParking.ParkingType; import com.example.Tools.FileManager; import com.example.Tools.ParkingTools; import com.example.Tools.PermissionManager; import com.example.intelligentparking.MainActivity; import com.example.intelligentparking.R; import com.google.android.material.navigation.NavigationView; import com.zxy.tiny.Tiny; import com.zxy.tiny.callback.FileWithBitmapCallback; import org.litepal.LitePal; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.IllegalFormatCodePointException; import java.util.List; import java.util.Locale; import de.hdodenhof.circleimageview.CircleImageView; public class Function extends AppCompatActivity { private LocationClient client; private View view = null; private View viewSex = null; private Button ToCamera,ToPhoto,ToExit,Exit; private PopupWindow popupWindow; private RecyclerView MyRecyclerView,HorizontalRecyclerView; private MyRecyclerViewTools addpter; private RecyclerViewHorizontalAdapter HorizontalAdapter; private EditText SearchPlace; private ImageButton Search; private ImageView UserIcon,GoBack; private Button IntentLoginInterface; private ViewPager viewPager; private TextToSpeech TTS = null; private TextView LoginTextStatus,LoginStatusTiltle,LoginStatusSubTiltle; private ImageView Scan; private NavigationView InfarmationManage; private CircleImageView Image; private TextView ID; private double Latitude = 0; private double Longitude = 0; public static final String BAIDUMAPPACKAGE = "com.baidu.BaiduMap"; // 百度地图包名 private List<ParkingTools> list = new ArrayList<>(); private List<MyRecyclerViewHorizontal> HorizontalList = new ArrayList<>( ); private static int OPENGALLERY = 1; private static int OPENCAMERA = 2; private static final String[] StatementPermisson = {Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE}; private static final int PERMISSION_CAMERA_REQUEST_CODE = 3; /*******************************以下为横向RecyclerView数据************************************/ public int[] BigImage = { R.drawable.zhuanpam, R.drawable.money20, R.drawable.stopcar, R.drawable.huafei20, R.drawable.meituan, R.drawable.money100 }; public int[] SmallImage = { R.drawable.go, R.drawable.go, R.drawable.go, R.drawable.go, R.drawable.go, R.drawable.go }; public String[] BigTitle = { "幸运大转盘", "20元停车券", "霸王免费停", "话费充值券", "最高88神券", "邀好友拿100" }; public String[] SmallTitle = { "天天抽好礼", "积分免费兑", "赢免费停车", "三网全通用", "外卖吃不完", "天天拿不完" }; /*******************************以上为横向RecyclerView数据************************************/ /*******************以下为停车位RecyclerView数据********************/ public String[] StringBigPlace = { "工人文化宫停车场", "停车场(康复路)", "停车场(水木兰庭东南)", "圣华名城停车场", "嵘兴.圣华名城停车场", "中车停车场", "停车场(杉塘路)" }; private String[] StringSmallPlace = { "田心大道52号附近", "响石岭步行街与建设北路交叉口东北100米", "建设北路200号附近", "建设北路36号附近", "杉塘路13号附近", "田心大道52号文化广场", "杉塘路与杉木塘路交叉口西南150米" }; private String[] StringDistance = { "232m", "1.8km", "1.9km", "1.9km", "2.0km", "233m", "2.1km" }; private int[] IntIcons = { R.drawable.lubiao, R.drawable.lubiao, R.drawable.lubiao, R.drawable.lubiao, R.drawable.lubiao, R.drawable.lubiao, R.drawable.lubiao }; /*******************以上为RecyclerView数据********************/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); /*******************一定要在setContentView()前调用********************/ SDKInitializer.initialize( getApplicationContext() ); client = new LocationClient( getApplicationContext() );//获取全局Context client.registerLocationListener( new MyBaiduMap() );//注册一个定位监听器,获取位置信息,回调此定位监听器 setContentView( R.layout.function ); InitData(); InitHorizontalData(); InitView(); InitAdapter(); InitHorizontalAdapter(); Listener(); //UpdateLayout(); InitNavigationView(); ShowPopupWindows(); /*******************语音助手初始化*******************/ TTS = new TextToSpeech( Function.this,new InitTTS() ); /*******************将所有需要判断的权限保存到一个数组中,统一一起判断*******************/ List<String> PermissionList = new ArrayList<>(); //判断权限是否授权 if (ContextCompat.checkSelfPermission( Function.this, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED) { PermissionList.add( Manifest.permission.ACCESS_FINE_LOCATION ); } if (ContextCompat.checkSelfPermission( Function.this, Manifest.permission.READ_PHONE_STATE ) != PackageManager.PERMISSION_GRANTED) { PermissionList.add( Manifest.permission.READ_PHONE_STATE ); } if (ContextCompat.checkSelfPermission( Function.this, Manifest.permission.WRITE_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED) { PermissionList.add( Manifest.permission.WRITE_EXTERNAL_STORAGE ); } if (!PermissionList.isEmpty()) { String[] Permissions = PermissionList.toArray( new String[PermissionList.size()] );//转化为数组 ActivityCompat.requestPermissions( Function.this, Permissions, 1 );//一次性申请权限 } else { /*****************如果权限都已经声明,开始配置参数*****************/ requestLocation(); } /*******************点击搜索按钮之后,进行语言播报*******************/ Search.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //根据搜索栏搜索的地点,然后根据自身的经纬度,进行查询 String EndAddress = SearchPlace.getText().toString(); String SpeakContent = "本次导航从当前位置前往"+EndAddress; String Zeng = "警告警告检测到周围有曾大屁出现快疏散人群她要放屁了她要放屁了她要放屁了重要的事情说三遍"; if (TTS != null && !TTS.isSpeaking()) { TTS.setPitch(0.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规 if (EndAddress.equals( "曾大屁" )) { TTS.speak(Zeng,TextToSpeech.QUEUE_FLUSH, null); }else { TTS.speak( SpeakContent, TextToSpeech.QUEUE_FLUSH, null ); } }else { Toast.makeText( Function.this, "语音助手错误", Toast.LENGTH_LONG ).show(); } if (isBaiduMapInstalled()) { Navigation(EndAddress); } else { // 未安装 Toast.makeText( Function.this, "您尚未安装百度地图", Toast.LENGTH_LONG ).show(); } } } ); } /******打开相册*/ public void openGallery() { Intent picture = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(picture, OPENGALLERY); } /************需要动态申请权限************/ /*********第一次需要动态申请,然后弹出一个提示是否允许启动相机********/ private void requestPermission() { // Android6.0开始, 即当API大于等于 23 时,才动态申请权限 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { this.requestPermissions(StatementPermisson, PERMISSION_CAMERA_REQUEST_CODE); } } /*****打开相机*/ public void OpenCamera(){ try { Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); File file = FileManager.createFileIfNeed("filepath.png"); Uri uri; if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) { uri = Uri.fromFile(file); } else { uri = FileProvider.getUriForFile(this, "link.android.file.provider", file); } intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); startActivityForResult(intent, 1); }catch (IOException e){ e.printStackTrace(); } } /********获取PopupWindows布局视图,并对其中选项进行监听事件书写*/ private void ShowPopupWindows() { //获取布局 view = LayoutInflater.from( Function.this).inflate( R.layout.popup_image,null,false ); ToCamera = view.findViewById( R.id.ToCamera ); //跳转相机 ToPhoto = view.findViewById( R.id.ToPhoto );//跳转相册 ToExit = view.findViewById( R.id.ToExit );//退出 popupWindow = new PopupWindow( view, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT ); popupWindow.setFocusable( true ); //获取焦点 popupWindow.setBackgroundDrawable( new BitmapDrawable( ) ); popupWindow.setOutsideTouchable( true ); //点击外面地方,取消 popupWindow.setTouchable( true ); //允许点击 popupWindow.setAnimationStyle( R.style.MyPopupWindow ); //设置动画 ToExit.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } } ); ToCamera.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { requestPermission(); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.addCategory(Intent.CATEGORY_DEFAULT); startActivityForResult(intent, OPENCAMERA); } } ); ToPhoto.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //打开相册 openGallery(); } } ); } /***************PopupWindows在底部显示************/ public void ShowPopupWindowsFromButtom(){ popupWindow.showAtLocation( view, Gravity.BOTTOM,0,0 ); } /*****************监听返回键*/ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){ if (popupWindow != null && popupWindow.isShowing()){ popupWindow.dismiss(); return true; } } return false; } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult( requestCode, resultCode, data ); //判断是否返回数据 if (resultCode == RESULT_OK) { //判断返回的类型是否是我们所需 if (requestCode == OPENGALLERY) { String state = Environment.getExternalStorageState(); if (!state.equals( Environment.MEDIA_MOUNTED )) return; Tiny.FileCompressOptions options = new Tiny.FileCompressOptions(); Tiny.getInstance().source( FileManager.readpic() ).asFile().withOptions( options ).compress( new FileWithBitmapCallback() { @Override public void callback(boolean isSuccess, Bitmap bitmap, String outfile, Throwable t) { //sendCropImage(outfile); } } ); } else if (requestCode == OPENCAMERA && resultCode == Activity.RESULT_OK && null != data) { Bundle bundle = data.getExtras(); Bitmap bitmap = (Bitmap) bundle.get("data"); Image.setImageBitmap(bitmap); } } } /*****************滑动页的初始化**********************/ private void InitNavigationView() { View view = InfarmationManage.getHeaderView( 0 ); Image = view.findViewById( R.id. HeadImage); ID = view.findViewById( R.id.Information_UserName ); /*如果登陆成功,则把注册的信息从数据库取出*/ Thread thread = new Thread( ()->{ runOnUiThread( ()->{ try { Thread.sleep( 2000 ); } catch (InterruptedException e) { e.printStackTrace(); } if (Login.JudgeLoginStatic == true) { ID.setText( Login.Login_Account ); IntentLoginInterface.setText( "已登陆" ); LoginTextStatus.setText("已登陆“布道者”智能停车,祝您生活愉快"); } else { ID.setText( "暂未登陆" ); } } ); } ); thread.start(); Image.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { ShowPopupWindowsFromButtom(); } } ); /****************滑动页中Menu中的子控件***************/ InfarmationManage.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() { Intent intent = null; @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()){ case R.id.Personal: intent = new Intent( Function.this,SettingInformation.class ); startActivity( intent ); break; case R.id.Pay: Toast.makeText( Function.this,"1",Toast.LENGTH_SHORT ).show(); break; case R.id.Car: Toast.makeText( Function.this,"2",Toast.LENGTH_SHORT ).show(); break; case R.id.Collection: startActivity( new Intent( Function.this, ParkingType.class ) ); break; case R.id.File: startActivity( new Intent( Function.this, Booking.class ) ); break; case R.id.Setting: Toast.makeText( Function.this,"5",Toast.LENGTH_SHORT ).show(); break; case R.id.Version: Toast.makeText( Function.this,"6",Toast.LENGTH_SHORT ).show(); break; /**订单管理模块*/ case R.id.OrderManagement: startActivity( new Intent( Function.this, OrderInterface.class ) ); break; } return true; } } ); } /**登陆后改变主页面相应布局*/ // private void UpdateLayout(){ // if (Login.JudgeLoginStatic == true){ // IntentLoginInterface.setText( "已登陆" ); // LoginTextStatus.setText( "已登陆“布道者”智能停车,祝您生活愉快" ); // } // } /**初始化RecyclerView数据*/ private void InitData() { for (int i = 0; i <7 ; i++) { ParkingTools tools = null; String BigPlace = StringBigPlace[i]; String SmallPlace = StringSmallPlace[i]; String Distance = StringDistance[i]; int Icon = IntIcons[i]; tools = new ParkingTools( BigPlace,SmallPlace,Icon,Distance ); list.add( tools ); } } /**初始化RecyclerView数据*/ private void InitHorizontalData() { for (int i = 0; i <6 ; i++) { MyRecyclerViewHorizontal tools = null; String StringBigTitle = BigTitle[i]; String StringSmallTitle = SmallTitle[i]; int IntBigImage = BigImage[i]; int IntSmallImage = SmallImage[i]; tools = new MyRecyclerViewHorizontal( StringBigTitle,StringSmallTitle,IntBigImage,IntSmallImage ); HorizontalList.add( tools ); } } private void InitHorizontalAdapter(){ LinearLayoutManager manager = new LinearLayoutManager( getApplicationContext(),RecyclerView.HORIZONTAL,false ); HorizontalRecyclerView.setLayoutManager( manager ); HorizontalAdapter = new RecyclerViewHorizontalAdapter( HorizontalList ); HorizontalRecyclerView.setAdapter( HorizontalAdapter ); } /***************RecyclerView适配器初始化*********************/ private void InitAdapter() { LinearLayoutManager manager = new LinearLayoutManager( getApplicationContext() ); MyRecyclerView.setLayoutManager( manager ); addpter = new MyRecyclerViewTools( list ); MyRecyclerView.setAdapter( addpter ); addpter.setOnclick( new MyRecyclerViewTools.OnClick() { String EndAddress = ""; @Override public void OnClickListener(View view, int Position) { if (Position == 0){ EndAddress = "工人文化宫停车场"; }else if (Position == 1){ EndAddress = "停车场(康复路)"; }else if (Position == 2){ EndAddress = "停车场(水木兰庭东南)"; }else if (Position == 3){ EndAddress = "圣华名城停车场"; }else if (Position == 4){ EndAddress = "嵘兴.圣华名城停车场"; }else if (Position == 5){ EndAddress = "中车停车场"; }else if (Position == 6){ EndAddress = "停车场(杉塘路)"; } String SpeakContent = "本次导航从当前位置前往"+EndAddress; if (TTS != null && !TTS.isSpeaking()) { TTS.setPitch(0.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规 TTS.speak(SpeakContent,TextToSpeech.QUEUE_FLUSH, null); }else { Toast.makeText( Function.this, "语音助手错误", Toast.LENGTH_LONG ).show(); } if (isBaiduMapInstalled()) { Navigation(EndAddress); } else { // 未安装 Toast.makeText( Function.this, "您尚未安装百度地图", Toast.LENGTH_LONG ).show(); } } } ); } /***************初始化界面*********************/ private void InitView() { MyRecyclerView = findViewById( R.id.MyRecyclerView ); HorizontalRecyclerView = findViewById( R.id.HorizontalRecyclerView ); Search = findViewById( R.id.Search ); SearchPlace = findViewById( R.id.SearchPlace ); // UserIcon = findViewById( R.id.useIcon ); IntentLoginInterface = findViewById( R.id.IntentLogin ); GoBack = findViewById( R.id.Function_goback ); Scan = findViewById( R.id.Scan ); LoginTextStatus = findViewById( R.id.LoginStatusText ); InfarmationManage = findViewById( R.id.InformationManage ); //LoginStatusSubTiltle = findViewById( R.id.LoginStatusSubTitle ); // LoginStatusTiltle = findViewById( R.id.LoginStatusTitle ); } //执行 private void requestLocation(){ initLocation(); client.start(); } /*******************初始化百度地图各种参数*******************/ private void initLocation() { LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); /**可选,设置定位模式,默认高精度LocationMode.Hight_Accuracy:高精度; * LocationMode. Battery_Saving:低功耗;LocationMode. Device_Sensors:仅使用设备;*/ option.setCoorType("bd09ll"); /**可选,设置返回经纬度坐标类型,默认gcj02gcj02:国测局坐标;bd09ll:百度经纬度坐标;bd09:百度墨卡托坐标; 海外地区定位,无需设置坐标类型,统一返回wgs84类型坐标*/ option.setScanSpan(3000); /**可选,设置发起定位请求的间隔,int类型,单位ms如果设置为0,则代表单次定位,即仅定位一次,默认为0如果设置非0,需设置1000ms以上才有效*/ option.setOpenGps(true); /**可选,设置是否使用gps,默认false使用高精度和仅用设备两种定位模式的,参数必须设置为true*/ option.setLocationNotify(true); /**可选,设置是否当GPS有效时按照1S/1次频率输出GPS结果,默认false*/ option.setIgnoreKillProcess(false); /**定位SDK内部是一个service,并放到了独立进程。设置是否在stop的时候杀死这个进程,默认(建议)不杀死,即setIgnoreKillProcess(true)*/ option.SetIgnoreCacheException(false); /**可选,设置是否收集Crash信息,默认收集,即参数为false*/ option.setIsNeedAltitude(true);/**设置海拔高度*/ option.setWifiCacheTimeOut(5 * 60 * 1000); /**可选,7.2版本新增能力如果设置了该接口,首次启动定位时,会先判断当前WiFi是否超出有效期,若超出有效期,会先重新扫描WiFi,然后定位*/ option.setEnableSimulateGps(false); /**可选,设置是否需要过滤GPS仿真结果,默认需要,即参数为false*/ option.setIsNeedAddress(true); /**可选,设置是否需要地址信息,默认不需要*/ client.setLocOption(option); /**mLocationClient为第二步初始化过的LocationClient对象需将配置好的LocationClientOption对象,通过setLocOption方法传递给LocationClient对象使用*/ } /****************监测是否安装了微信***************/ public static boolean CheckWeiXinInstalled(Context context) { final PackageManager packageManager = context.getPackageManager();// 获取packagemanager List<PackageInfo> pinfo = packageManager.getInstalledPackages( 0 );// 获取所有已安装程序的包信息 if (pinfo != null) { for (int i = 0; i < pinfo.size(); i++) { String pn = pinfo.get( i ).packageName; if (pn.equals( "com.tencent.mm" )) { return true; } } } return false; } /****************监测支付宝是否安装***************/ public static boolean CheckAliPayInstalled(Context context) { Uri uri = Uri.parse("alipays://platformapi/startApp"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); ComponentName componentName = intent.resolveActivity(context.getPackageManager()); return componentName != null; } /****************跳转到微信扫一扫***************/ private void toWeiXinScan(Context context){ try { Intent intent = Function.this.getPackageManager().getLaunchIntentForPackage( "com.tencent.mm" ); intent.putExtra( "LauncherUI.From.Scaner.Shortcut", true ); startActivity( intent ); } catch (Exception e) { Toast.makeText( Function.this, "没有安装微信", Toast.LENGTH_SHORT ).show(); } } /****************跳转到支付宝扫一扫***************/ public static void toAliPayScan(Context context) { try { Uri uri = Uri.parse("alipayqr://platformapi/startapp?saId=10000007"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); context.startActivity(intent); } catch (Exception e) { Toast.makeText(context, "打开失败,请检查是否安装了支付宝", Toast.LENGTH_SHORT).show(); } } //监听事件 private void Listener(){ OnClick onClick = new OnClick(); //Search.setOnClickListener( onClick ); IntentLoginInterface.setOnClickListener( onClick ); // UserIcon.setOnClickListener( onClick ); GoBack.setOnClickListener( onClick ); Scan.setOnClickListener( onClick ); } /****************内部类监听事件***************/ class OnClick implements View.OnClickListener{ Intent intent = null; TTS tts = null; // 语音助手类 @Override public void onClick(View v) { switch (v.getId()) { case R.id.IntentLogin: if (Login.JudgeLoginStatic == false) { intent = new Intent( Function.this, Login.class ); }else{ IntentLoginInterface.setText( "已登陆" ); LoginTextStatus.setText("已登陆“布道者”智能停车,祝您生活愉快"); ID.setText( Login.Login_Account); return; } break; //跳转到桌面i case R.id.Function_goback: intent = new Intent( Intent.ACTION_MAIN ); intent.addCategory( Intent.CATEGORY_HOME ); finish(); break; case R.id.Scan: if (CheckWeiXinInstalled( Function.this ) == true) { toWeiXinScan( Function.this ); } else if (CheckAliPayInstalled( Function.this ) == true) { toAliPayScan( Function.this ); } else { Toast.makeText( Function.this, "未安装微信或者支付宝,无法进行支付", Toast.LENGTH_SHORT ).show(); } break; } startActivity( intent ); } } /****************用于跳转到百度地图***************/ public void Navigation(String EndAddress){ Uri uri = Uri.parse( "baidumap://map/direction?origin=" + Latitude + "," + Longitude + "&" + "destination=" + EndAddress + "&mode=driving&package=com.baidu.BaiduMap;end" ); startActivity( new Intent( Intent.ACTION_VIEW, uri ) ); } /****************判断百度地图是否安装***************/ public static boolean isBaiduMapInstalled(){ return isInstallPackage(BAIDUMAPPACKAGE); } private static boolean isInstallPackage(String packageName) { return new File("/data/data/" + packageName).exists(); } class MyBaiduMap implements BDLocationListener{ @Override public void onReceiveLocation(BDLocation bdLocation) { Latitude = bdLocation.getLatitude();//获取纬度 Longitude = bdLocation.getLongitude();//获取经度 double Radius = bdLocation.getRadius(); } } /****************语音助手监听事件***************/ class InitTTS implements TextToSpeech.OnInitListener{ @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { int result = TTS.setLanguage( Locale.CHINA); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { Toast.makeText( Function.this, "数据丢失或不支持", Toast.LENGTH_SHORT).show(); } } } } }
总结
实践出真知