提交 6f59180a 编写于 作者: 杜庆泉's avatar 杜庆泉

内置函数迁移至 io.dcloud.uts#UTSAndroid

上级 e941620d
......@@ -23,22 +23,9 @@ import DialogInterface from 'android.content.DialogInterface';
import EditText from 'android.widget.EditText';
import {
onAppActivityDestroy,
offAppActivityDestroy,
onAppActivityPause,
offAppActivityPause,
onAppActivityResume,
offAppActivityResume,
onAppActivityBack,
offAppActivityBack,
onAppActivityResult,
onAppTrimMemory,
offAppTrimMemory,
onAppConfigChange,
offAppConfigChange,
getUniActivity,
getAppContext
} from "io.dcloud.uts.android";
UTSAndroid
} from "io.dcloud.uts";
/**
......@@ -101,14 +88,14 @@ class AddUIRunnable extends Runnable {
override run():void {
let textView = new TextView(getUniActivity())
let textView = new TextView(UTSAndroid.getUniActivity())
textView.setText("HELLO WORLD");
textView.textSize = 30.0.toFloat();
textView.setBackgroundColor(Color.RED)
textView.setTag("helloText")
textView.setGravity(Gravity.CENTER)
let decorView = getUniActivity()!.window.decorView;
let decorView = UTSAndroid.getUniActivity()!.window.decorView;
let frameContent = decorView.findViewById<FrameLayout>(android.R.id.content)
......@@ -128,7 +115,7 @@ class RemoveUIRunnable extends Runnable {
override run():void {
let decorView = getUniActivity()!.getWindow().getDecorView();
let decorView = UTSAndroid.getUniActivity()!.getWindow().getDecorView();
let frameContent = decorView.findViewById<FrameLayout>(android.R.id.content)
let targetTV = frameContent.findViewWithTag<TextView>("helloText")
......@@ -143,7 +130,7 @@ class RemoveUIRunnable extends Runnable {
*/
export function addViewToDecorView() {
let uiRunable = new AddUIRunnable();
getUniActivity()!.runOnUiThread(uiRunable)
UTSAndroid.getUniActivity()!.runOnUiThread(uiRunable)
}
/**
......@@ -151,7 +138,7 @@ export function addViewToDecorView() {
*/
export function removeViewToDecorView() {
var uiRunable = new RemoveUIRunnable();
getUniActivity()!.runOnUiThread(uiRunable)
UTSAndroid.getUniActivity()!.runOnUiThread(uiRunable)
}
/**
......@@ -172,7 +159,7 @@ class DialogListener extends DialogInterface.OnClickListener{
//数据获取
let input = this.inputET.getText().toString()
this.callback(input);
Toast.makeText(getUniActivity(),input,
Toast.makeText(UTSAndroid.getUniActivity(),input,
Toast.LENGTH_LONG).show();
}
......@@ -193,10 +180,10 @@ class DialogUIRunnable extends Runnable {
override run():void {
let et = new EditText(getUniActivity());
let et = new EditText(UTSAndroid.getUniActivity());
et.setText("127.0.0.1");
new AlertDialog.Builder(getUniActivity()).setTitle("请输入IP地址")
new AlertDialog.Builder(UTSAndroid.getUniActivity()).setTitle("请输入IP地址")
.setIcon(android.R.drawable.ic_dialog_info).setView(et)
.setPositiveButton("确定", new DialogListener(et,this.callback))
.setNegativeButton("取消", null).show();
......@@ -212,7 +199,7 @@ class DialogUIRunnable extends Runnable {
export function getUserInput(success: (res: string) => void) {
let uiRunable = new DialogUIRunnable(success);
getUniActivity()!.runOnUiThread(uiRunable)
UTSAndroid.getUniActivity()!.runOnUiThread(uiRunable)
}
......@@ -224,8 +211,8 @@ export function getUserInput(success: (res: string) => void) {
*/
export function getMetaConfig(): string {
//
let packageName = getAppContext()!.getPackageName();
let appInfo = getAppContext()!.getPackageManager()!.getApplicationInfo(packageName,PackageManager.GET_META_DATA)
let packageName = UTSAndroid.getAppContext()!.getPackageName();
let appInfo = UTSAndroid.getAppContext()!.getPackageManager()!.getApplicationInfo(packageName,PackageManager.GET_META_DATA)
let metaData = appInfo.metaData
if (metaData == null) {
......@@ -262,7 +249,7 @@ let globalPlayer:MediaPlayer| null = null;
*/
export function playAssetAudio() {
let assetManager = getAppContext()!.getAssets();
let assetManager = UTSAndroid.getAppContext()!.getAssets();
let afd = assetManager.openFd("free.mp3");
if(globalPlayer == null){
......@@ -290,21 +277,21 @@ export function stopAssetAudio() {
export function goOtherActivity(imageDone: (event:string) => void):boolean {
// 检查相关权限是否已经具备
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// 不具备权限,申请权限,并且告知用户监听失败
ActivityCompat.requestPermissions(getUniActivity()!, arrayOf(Manifest.permission.CAMERA), 1002)
ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, arrayOf(Manifest.permission.CAMERA), 1002)
return false;
}
onAppActivityResult((requestCode: Int, resultCode: Int, data?: Intent) => {
UTSAndroid.onAppActivityResult((requestCode: Int, resultCode: Int, data?: Intent) => {
let eventName = "onAppActivityResult - requestCode:" + requestCode + " -resultCode:"+resultCode + " -data:"+JSON.stringify(data);
console.log(eventName);
if ((requestCode == 1001) && (resultCode == Activity.RESULT_OK)) {
if (data != null) {
let bundle = data.getExtras();
let mImageBitmap = bundle!.get("data") as Bitmap;
let bitmapPath = getUniActivity()!.getExternalCacheDir()!.getPath() + "/photo.png"
let bitmapPath = UTSAndroid.getUniActivity()!.getExternalCacheDir()!.getPath() + "/photo.png"
console.log(bitmapPath);
try{
mImageBitmap.compress(Bitmap.CompressFormat.PNG,100,new FileOutputStream(bitmapPath))
......@@ -318,8 +305,8 @@ export function goOtherActivity(imageDone: (event:string) => void):boolean {
let takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//resolveActivity 返回可处理 Intent 的第一个 Activity 组件
if (takePictureIntent.resolveActivity(getUniActivity()!.getPackageManager()) != null) {
getUniActivity()!.startActivityForResult(takePictureIntent, 1001);
if (takePictureIntent.resolveActivity(UTSAndroid.getUniActivity()!.getPackageManager()) != null) {
UTSAndroid.getUniActivity()!.startActivityForResult(takePictureIntent, 1001);
}
return true;
......@@ -338,7 +325,7 @@ export function initAppLifecycle(onLifecycleChange: (event:string) => void) {
* application 内存不足的回调函数
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onapptrimmemory
*/
onAppTrimMemory((level:Number) => {
UTSAndroid.onAppTrimMemory((level:Number) => {
let eventName = "onAppTrimMemory - " + level;
onLifecycleChange(eventName);
console.log(eventName);
......@@ -348,7 +335,7 @@ export function initAppLifecycle(onLifecycleChange: (event:string) => void) {
* application 状态改变的回调函数
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onAppConfigChange
*/
onAppConfigChange((ret:UTSJSONObject) => {
UTSAndroid.onAppConfigChange((ret:UTSJSONObject) => {
let eventName = "onAppConfigChange - " + JSON.stringify(ret);
onLifecycleChange(eventName);
console.log(eventName);
......@@ -359,7 +346,7 @@ export function initAppLifecycle(onLifecycleChange: (event:string) => void) {
* activity 销毁生命周期回调
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitydestroy
*/
onAppActivityDestroy(() => {
UTSAndroid.onAppActivityDestroy(() => {
let eventName = "onAppActivityDestroy";
onLifecycleChange(eventName);
console.log(eventName);
......@@ -370,7 +357,7 @@ export function initAppLifecycle(onLifecycleChange: (event:string) => void) {
* activity 失去焦点生命周期回调
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitypause
*/
onAppActivityPause(() => {
UTSAndroid.onAppActivityPause(() => {
let eventName = "onAppActivityPause" ;
onLifecycleChange(eventName);
console.log(eventName);
......@@ -379,7 +366,7 @@ export function initAppLifecycle(onLifecycleChange: (event:string) => void) {
* activity 得到焦点的周期回调
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityresume
*/
onAppActivityResume(() => {
UTSAndroid.onAppActivityResume(() => {
let eventName = "onAppActivityResume";
onLifecycleChange(eventName);
console.log(eventName);
......@@ -388,7 +375,7 @@ export function initAppLifecycle(onLifecycleChange: (event:string) => void) {
* activity 回退物理按键事件回调
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityback
*/
onAppActivityBack(() => {
UTSAndroid.onAppActivityBack(() => {
let eventName = "onAppActivityBack";
onLifecycleChange(eventName);
console.log(eventName);
......@@ -406,30 +393,30 @@ export function unRegLifecycle() {
/**
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onapptrimmemory
*/
offAppTrimMemory();
UTSAndroid.offAppTrimMemory();
/**
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onAppConfigChange
*/
offAppConfigChange();
UTSAndroid.offAppConfigChange();
/**
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitydestroy
*/
offAppActivityDestroy();
UTSAndroid.offAppActivityDestroy();
/**
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitypause
*/
offAppActivityPause();
UTSAndroid.offAppActivityPause();
/**
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityresume
*/
offAppActivityResume();
UTSAndroid.offAppActivityResume();
/**
* activity 回退物理按键事件回调
*/
offAppActivityBack();
UTSAndroid.offAppActivityBack();
}
\ No newline at end of file
export function showAlert(title: string|null, message: string|null, result: (index: Number) => void) {}
export function showPrompt(title: string|null, message: string|null,placeholder: string|null, result: (content: string)=>void) {}
@Suppress("unuse")
export function showAlert(_title: string|null, _message: string|null, _result: (index: Number) => void) {
// todo
}
@Suppress("unuse")
export function showPrompt(_title: string|null,_message: string|null,_placeholder: string|null, _result: (content: string)=>void) {
// todo
}
......@@ -18,9 +18,8 @@ import Toast from 'android.widget.Toast';
import {
getUniActivity,
getAppContext
} from "io.dcloud.uts.android";
UTSAndroid
} from "io.dcloud.uts";
import Service from 'android.app.Service';
import System from 'java.lang.System';
......@@ -67,7 +66,7 @@ class ForeService extends Service {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
let manager =
getAppContext()!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
UTSAndroid.getAppContext()!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
let channelId = "channelId" + System.currentTimeMillis()
let channel = new NotificationChannel(
channelId,
......@@ -115,12 +114,12 @@ class StartServiceListener extends OnClickListener{
override onClick(v?: View):void{
var intent = new Intent(getUniActivity(), ForeService().javaClass);
var intent = new Intent(UTSAndroid.getUniActivity(), ForeService().javaClass);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
getAppContext()!.startForegroundService(intent);
UTSAndroid.getAppContext()!.startForegroundService(intent);
}
else{
getAppContext()!.startService(intent);
UTSAndroid.getAppContext()!.startService(intent);
}
}
......@@ -135,10 +134,10 @@ class StartBroadcastListener extends OnClickListener{
let filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
getUniActivity()!.registerReceiver(myReceiver, filter);
UTSAndroid.getUniActivity()!.registerReceiver(myReceiver, filter);
// 提示屏幕状态监听已经注册
Toast.makeText(getAppContext(),"屏幕状态监听已注册,注意观察控制台日志",Toast.LENGTH_LONG).show();
Toast.makeText(UTSAndroid.getAppContext(),"屏幕状态监听已注册,注意观察控制台日志",Toast.LENGTH_LONG).show();
}
}
......@@ -148,7 +147,7 @@ class StartThreadListener extends OnClickListener{
override onClick(v?: View):void{
new CustomThread().start()
// 提示屏幕状态监听已经注册
Toast.makeText(getAppContext(),"自定义线程已开启,注意观察控制台输出",Toast.LENGTH_LONG).show();
Toast.makeText(UTSAndroid.getAppContext(),"自定义线程已开启,注意观察控制台输出",Toast.LENGTH_LONG).show();
}
}
......@@ -191,18 +190,19 @@ class DemoActivity extends Activity{
}
@Suppress("DEPRECATION")
class IntentRunable extends Runnable{
override run(){
console.log("IntentRunable = " + Thread.currentThread().getName())
let intent = new Intent(getUniActivity(),DemoActivity().javaClass);
getUniActivity()!.startActivity(intent);
let intent = new Intent(UTSAndroid.getUniActivity(),DemoActivity().javaClass);
UTSAndroid.getUniActivity()!.startActivity(intent);
}
}
@Suppress("DEPRECATION")
export default function gotoDemoActivity() {
// 跳转到一个新的界面
getUniActivity()!.runOnUiThread(new IntentRunable())
UTSAndroid.getUniActivity()!.runOnUiThread(new IntentRunable())
return { name: "gotoDemoActivity"};
}
......
import {
getUniActivity
} from "io.dcloud.uts.android";
UTSAndroid
} from "io.dcloud.uts";
import ActivityCompat from "androidx.core.app.ActivityCompat";
......@@ -84,12 +84,13 @@ class ScreenFileObserver extends FileObserver {
/**
* 开启截图监听
*/
@Suppress("DEPRECATION")
export function onUserCaptureScreen(callback: (res:object) => void) {
// 检查相关权限是否已经具备
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// 不具备权限,申请权限,并且告知用户监听失败
ActivityCompat.requestPermissions(getUniActivity()!, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1001)
ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1001)
// 因权限缺失导致监听失败
let ret = {
......
import {
onAppActivityRequestPermissionsResult,
getUniActivity,
getAppContext
} from "io.dcloud.uts.android";
UTSAndroid
} from "io.dcloud.uts";
import ActivityCompat from "androidx.core.app.ActivityCompat";
import Manifest from "android.Manifest";
......@@ -23,7 +21,7 @@ export function requestPremission() {
TencentLocationManager.setUserAgreePrivacy(true);
// 注册一个请求回调
onAppActivityRequestPermissionsResult((requestCode: number,
UTSAndroid.onAppActivityRequestPermissionsResult((requestCode: number,
permissions: MutableList<string>,
grantResults: MutableList<number>) => {
/**
......@@ -37,7 +35,7 @@ export function requestPremission() {
// 发起权限申请
ActivityCompat.requestPermissions(
getUniActivity()!,
UTSAndroid.getUniActivity()!,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION), 1001);
// 请求权限
......@@ -127,8 +125,8 @@ class SingleLocationListener extends TencentLocationListener {
*/
function checkLocationConfig():boolean{
let packageName = getAppContext()!.getPackageName();
let appInfo = getAppContext()!.getPackageManager()!.getApplicationInfo(packageName,PackageManager.GET_META_DATA)
let packageName = UTSAndroid.getAppContext()!.getPackageName();
let appInfo = UTSAndroid.getAppContext()!.getPackageManager()!.getApplicationInfo(packageName,PackageManager.GET_META_DATA)
let metaData = appInfo.metaData
if (metaData == null) {
......@@ -159,7 +157,7 @@ export function getLocation(locationOptions: LocationOptions):boolean {
return false
}
let mLocationManager = TencentLocationManager.getInstance(getAppContext());
let mLocationManager = TencentLocationManager.getInstance(UTSAndroid.getAppContext());
// 定位监听器封装
let locationOptionWrapper = new LocationOptionsWapper(locationOptions);
let mLocationListener = new SingleLocationListener(locationOptionWrapper);
......@@ -179,4 +177,17 @@ export function getLocation(locationOptions: LocationOptions):boolean {
return true;
}
/**
* 持续监听位置变化
*/
@Suppress("unuse")
export function watchPosition(_locationOptions: LocationOptions) {
//todo
}
/**
* 关闭监听位置变化
*/
export function clearWatch() {
//todo
}
\ No newline at end of file
import {
getUniActivity
} from "io.dcloud.uts.android";
UTSAndroid
} from "io.dcloud.uts";
import XToast from "com.hjq.xtoast.XToast";
import R from "io.dcloud.uni_modules.uts_toast.R";
......@@ -13,7 +13,7 @@ class UIRunnable extends Runnable {
override run():void {
new XToast<XToast<unknown>>(getUniActivity())
new XToast<XToast<unknown>>(UTSAndroid.getUniActivity())
.setDuration(1000)
.setContentView(R.layout.window_hint)
.setImageDrawable(android.R.id.icon, android.R.mipmap.sym_def_app_icon)
......@@ -38,6 +38,6 @@ export function showToast():boolean{
}
let toastRunable = new UIRunnable();
getUniActivity()!.runOnUiThread(toastRunable)
UTSAndroid.getUniActivity()!.runOnUiThread(toastRunable)
return true
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册