index.uts 8.6 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import Activity from 'android.app.Activity';
import BroadcastReceiver from 'android.content.BroadcastReceiver';
import Notification from 'android.app.Notification';
import Bundle from 'android.os.Bundle';
import R from 'io.dcloud.uni_modules.uts_nativepage.R';
import Intent from 'android.content.Intent';
import IntentFilter from 'android.content.IntentFilter';
import NotificationCompat from 'androidx.core.app.NotificationCompat';
import NotificationManager from 'android.app.NotificationManager';
import NotificationChannel from 'android.app.NotificationChannel';
import Context from 'android.content.Context';
import Button from 'android.widget.Button';
import OnClickListener from 'android.view.View.OnClickListener';
import View from 'android.view.View';
import Build from 'android.os.Build';
import IBinder from 'android.os.IBinder';
import Toast from 'android.widget.Toast';

杜庆泉's avatar
杜庆泉 已提交
19

杜庆泉's avatar
杜庆泉 已提交
20 21
import Service from 'android.app.Service';
import System from 'java.lang.System';
杜庆泉's avatar
杜庆泉 已提交
22
import Looper from 'android.os.Looper';
打打卡夫卡's avatar
打打卡夫卡 已提交
23 24 25 26
import Class from 'java.lang.Class';
import Exception from 'java.lang.Exception';
import PackageManager from 'android.content.pm.PackageManager';
import ResolveInfo from 'android.content.pm.ResolveInfo';
27 28 29 30 31
import RecyclerView from 'androidx.recyclerview.widget.RecyclerView';
import TextView from 'android.widget.TextView';
import ViewGroup from 'android.view.ViewGroup';
import LayoutInflater from 'android.view.LayoutInflater';
import LinearLayoutManager from 'androidx.recyclerview.widget.LinearLayoutManager';
杜庆泉's avatar
杜庆泉 已提交
32
export {DoAppWidget} from "./DoAppWidget.uts"
杜庆泉's avatar
杜庆泉 已提交
33

杜庆泉's avatar
杜庆泉 已提交
34 35 36 37 38 39 40 41 42 43 44 45
class ForeService extends Service {

	constructor (){
		super();
	}

    override onCreate():void {
        super.onCreate();
		console.log("onCreate");
    }
	

杜庆泉's avatar
杜庆泉 已提交
46
	override onBind(_intent?: Intent): IBinder|null{
杜庆泉's avatar
杜庆泉 已提交
47 48 49 50 51
		return null;
    }
	
    override onStartCommand(intent:Intent ,flags:Int ,startId:Int ):Int {

52
        let mBuilder = new NotificationCompat.Builder(this,"uts-test");
杜庆泉's avatar
杜庆泉 已提交
53
		
杜庆泉's avatar
杜庆泉 已提交
54 55
		// 点击后让通知将消失
		mBuilder.setAutoCancel(true) 
杜庆泉's avatar
杜庆泉 已提交
56 57
	    mBuilder.setContentText("测试")
	    mBuilder.setContentTitle("测试")
杜庆泉's avatar
杜庆泉 已提交
58 59 60 61 62 63 64 65
		//通知产生的时间,会在通知信息里显示
	    mBuilder.setWhen(System.currentTimeMillis()) 
	    //设置该通知优先级
	    mBuilder.setPriority(NotificationManager.IMPORTANCE_DEFAULT) 
		//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
	    mBuilder.setOngoing(false) 
		//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:
	    mBuilder.setDefaults(Notification.DEFAULT_ALL) 
杜庆泉's avatar
杜庆泉 已提交
66 67 68 69 70
	          
			   
	   	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
	          
			let manager =
71
				UTSAndroid.getAppContext()!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
杜庆泉's avatar
杜庆泉 已提交
72 73 74 75 76 77 78 79 80 81 82
			let channelId = "channelId" + System.currentTimeMillis()
			let channel = new NotificationChannel(
				channelId,
				"appName",
				NotificationManager.IMPORTANCE_HIGH
			)
			manager.createNotificationChannel(channel)
			mBuilder.setChannelId(channelId)
		}
	   				
	    mBuilder.setContentIntent(null)
杜庆泉's avatar
杜庆泉 已提交
83
	    this.startForeground(102, mBuilder.build())
杜庆泉's avatar
杜庆泉 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97

        return super.onStartCommand(intent, flags, startId);
    }

    override onDestroy():void {
        super.onDestroy();
        this.stopForeground(true);// 停止前台服务--参数:表示是否移除之前的通知
    }


}



杜庆泉's avatar
杜庆泉 已提交
98
class ScreenReceiver extends BroadcastReceiver{
杜庆泉's avatar
杜庆泉 已提交
99
	
杜庆泉's avatar
杜庆泉 已提交
100 101 102 103
	constructor (){
		super();
	}
	
杜庆泉's avatar
杜庆泉 已提交
104
   override onReceive(_context: Context, intent: Intent):void {
杜庆泉's avatar
杜庆泉 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118
        if (Intent.ACTION_SCREEN_OFF == intent.action) {
            console.log("==屏幕休眠了")
        }
        if (Intent.ACTION_SCREEN_ON == intent.action) {
            console.log("==屏幕唤醒了")
        }
    }
}


class StartServiceListener extends OnClickListener{
	
    override onClick(v?: View):void{
		
119
		var intent = new Intent(UTSAndroid.getUniActivity(), ForeService().javaClass);
杜庆泉's avatar
杜庆泉 已提交
120
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
121
			UTSAndroid.getAppContext()!.startForegroundService(intent);
杜庆泉's avatar
杜庆泉 已提交
122 123
		}
		else{
124
			UTSAndroid.getAppContext()!.startService(intent);
杜庆泉's avatar
杜庆泉 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138
		}

    }
}


class StartBroadcastListener extends OnClickListener{
	
    override onClick(v?: View):void{
		
		let myReceiver = new ScreenReceiver();
		let filter = new IntentFilter();
		filter.addAction(Intent.ACTION_SCREEN_OFF);
		filter.addAction(Intent.ACTION_SCREEN_ON);
139
		UTSAndroid.getUniActivity()!.registerReceiver(myReceiver, filter);
杜庆泉's avatar
杜庆泉 已提交
140 141
		
		// 提示屏幕状态监听已经注册
142
		Toast.makeText(UTSAndroid.getAppContext(),"屏幕状态监听已注册,注意观察控制台日志",Toast.LENGTH_LONG).show();
杜庆泉's avatar
杜庆泉 已提交
143 144 145 146
		
    }
}

杜庆泉's avatar
杜庆泉 已提交
147 148 149 150 151
class StartThreadListener extends OnClickListener{
	
    override onClick(v?: View):void{
		new CustomThread().start()
		// 提示屏幕状态监听已经注册
152
		Toast.makeText(UTSAndroid.getAppContext(),"自定义线程已开启,注意观察控制台输出",Toast.LENGTH_LONG).show();
杜庆泉's avatar
杜庆泉 已提交
153 154 155
		
    }
}
杜庆泉's avatar
杜庆泉 已提交
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

class ActivityFinishListener extends OnClickListener{
	
	host:Activity
	
	constructor(activity:Activity){
		super()
		this.host = activity
	}
	
    override onClick(v?: View):void{
		Toast.makeText(UTSAndroid.getAppContext(),"手动关闭当前activity",Toast.LENGTH_LONG).show();
		this.host.finish()
		
    }
}

杜庆泉's avatar
杜庆泉 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187
/**
 * 自定义异步线程
 */
class CustomThread extends Thread{
	
	constructor(){
		super();
	}
	
	override run(){
		Thread.sleep(1000)
		console.log("CustomThread = " + Thread.currentThread().getName())
	}
}
杜庆泉's avatar
杜庆泉 已提交
188

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228


class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{
	
	
	mFruitList: Array<String>
	
	constructor(param:Array<String>){
		mFruitList = param
	}
	
	ViewHolder = class extends RecyclerView.ViewHolder {
		
		fruitName:TextView;
		constructor(view:View) {
			super(view);
			fruitName = view.findViewById(R.id.fruit_name) as TextView;
		}
	}

    override onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        let view: View =
            LayoutInflater.from(parent.context).inflate(R.layout.fruit_item, parent, false)
        return ViewHolder(view)
    }

    override onBindViewHolder(holder: ViewHolder, position: Int) {
        let fruit: String = mFruitList[position]
        holder.fruitName.setText(fruit)
    }

    override getItemCount(): Int {
        return mFruitList.size
    }
	
}

杜庆泉's avatar
杜庆泉 已提交
229
class DemoActivity  extends Activity{
杜庆泉's avatar
杜庆泉 已提交
230
	
杜庆泉's avatar
杜庆泉 已提交
231 232 233
	constructor (){
		super();
	}
杜庆泉's avatar
杜庆泉 已提交
234 235 236
	
	override onCreate(savedInstanceState?: Bundle):void {
		super.onCreate(savedInstanceState)
杜庆泉's avatar
杜庆泉 已提交
237
		this.setContentView(R.layout.demo_activity);
杜庆泉's avatar
杜庆泉 已提交
238 239 240 241 242 243 244
		
		 let btn_start_service_front = findViewById<Button>(R.id.btn_start_service_front);
		 btn_start_service_front.setOnClickListener(new StartServiceListener());
		 
		 let btn_start_screen_listen = findViewById<Button>(R.id.btn_start_screen_listen);
		 btn_start_screen_listen.setOnClickListener(new StartBroadcastListener());
		 
杜庆泉's avatar
杜庆泉 已提交
245 246 247
		 let btn_start_thread = findViewById<Button>(R.id.btn_start_thread);
		 btn_start_thread.setOnClickListener(new StartThreadListener());
		 
248 249 250 251
		 let btn_finish_activity = findViewById<Button>(R.id.btn_finish_activity);
		 btn_finish_activity.setOnClickListener(new ActivityFinishListener(this));
		 
		 
252 253 254 255
		 
		 let recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
		 let layoutManager = LinearLayoutManager(this);
		 recyclerView.setLayoutManager(layoutManager);
杜庆泉's avatar
杜庆泉 已提交
256
		 let fruitAdapter = FruitAdapter(utsArrayOf("香蕉","苹果","大鸭梨"));
257
		 recyclerView.setAdapter(fruitAdapter);
杜庆泉's avatar
杜庆泉 已提交
258 259 260 261
	}
	
}

262
@Suppress("DEPRECATION")
杜庆泉's avatar
杜庆泉 已提交
263 264 265
class IntentRunable extends Runnable{
	override run(){
		console.log("IntentRunable = " + Thread.currentThread().getName())
266 267
		let intent = new Intent(UTSAndroid.getUniActivity(),DemoActivity().javaClass);
		UTSAndroid.getUniActivity()!.startActivity(intent);
杜庆泉's avatar
杜庆泉 已提交
268 269 270
	}
}

271
@Suppress("DEPRECATION")
打打卡夫卡's avatar
打打卡夫卡 已提交
272
export default function gotoDemoActivity():boolean {
273
	// 这里的逻辑是为了判断 当前的自定义activity 是否注册了,并以此为条件判断是否是自定义基座
打打卡夫卡's avatar
打打卡夫卡 已提交
274 275 276 277
	let hasXActivityIntegration =  true
	try{
		let packageManager = UTSAndroid.getUniActivity()!.getPackageManager();
		let intent = new Intent(UTSAndroid.getUniActivity(),DemoActivity().javaClass);
杜庆泉's avatar
杜庆泉 已提交
278
		let resolveInfo = packageManager.queryIntentActivities(intent,0);
279
		console.log(resolveInfo.size)
打打卡夫卡's avatar
打打卡夫卡 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
		if(resolveInfo.size == 0){
			hasXActivityIntegration = false;
		}
	}catch(e:Exception){
		console.log(e);
		hasXActivityIntegration = false;
	}
	
	if(!hasXActivityIntegration){
		return false;
	}
	// 跳转到一个新的界面
	UTSAndroid.getUniActivity()!.runOnUiThread(new IntentRunable())
	return true;
 
杜庆泉's avatar
杜庆泉 已提交
295 296 297
}