index.uts 5.0 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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';

import {
	getUniActivity,
	getAppContext
} from "io.dcloud.uts.android";

import Service from 'android.app.Service';
import System from 'java.lang.System';
27
import WindowManager from 'android.view.WindowManager';
杜庆泉's avatar
杜庆泉 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

class ForeService extends Service {



	constructor (){
		super();
	}

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

	override onBind(intent?: Intent): IBinder|null{
		return null;
    }
	
    override onStartCommand(intent:Intent ,flags:Int ,startId:Int ):Int {

49
        let mBuilder = new NotificationCompat.Builder(this,"uts-test");
杜庆泉's avatar
杜庆泉 已提交
50 51 52 53 54 55
		
		mBuilder.setAutoCancel(true) // 点击后让通知将消失
	    mBuilder.setContentText("测试")
	    mBuilder.setContentTitle("测试")
	    mBuilder.setWhen(System.currentTimeMillis()) //通知产生的时间,会在通知信息里显示
	         
56
	    mBuilder.setPriority(NotificationManager.IMPORTANCE_DEFAULT) //设置该通知优先级
杜庆泉's avatar
杜庆泉 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	    mBuilder.setOngoing(false) //ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
	    mBuilder.setDefaults(Notification.DEFAULT_ALL) //向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:
	          
			   
	   	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
	          
			let manager =
				getAppContext()!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
			let channelId = "channelId" + System.currentTimeMillis()
			let channel = new NotificationChannel(
				channelId,
				"appName",
				NotificationManager.IMPORTANCE_HIGH
			)
			manager.createNotificationChannel(channel)
			mBuilder.setChannelId(channelId)
		}
	   				
	    mBuilder.setContentIntent(null)
	    this.startForeground(222, mBuilder.build())

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

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


}



class ScreenReceiver extends BroadcastReceiver() {
	
    override onReceive(context: Context, intent: Intent):void {
        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{
		
		var intent = Intent(getUniActivity(), ForeService().javaClass);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			getAppContext()!.startForegroundService(intent);
		}
		else{
			getAppContext()!.startService(intent);
		}

    }
}


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);
		getUniActivity()!.registerReceiver(myReceiver, filter);
		
		// 提示屏幕状态监听已经注册
		Toast.makeText(getAppContext(),"屏幕状态监听已注册,注意观察控制台日志",Toast.LENGTH_LONG).show();
		
    }
}




class DemoActivity  extends Activity(){
	
	
	override onCreate(savedInstanceState?: Bundle):void {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.demo_activity);
		
		
		 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());
		 
	}
	
}


export default function gotoDemoActivity() {
  // 跳转到一个新的界面
  let intent = new Intent(getUniActivity(),DemoActivity().javaClass);
  getUniActivity()!.startActivity(intent);
  
  // var a = new DemoActivity();
  return { name: "gotoDemoActivity"};
}