android调用jni操作指示灯

87 篇文章 0 订阅

分享一下自己的笔记, android调用jni操作指示灯,android直接操作工作灯和电影灯 。 

 public native int onPowerLed(); //开电源灯

 public native int offPowerLed();//关电源灯

 public native int onWorkLed();//开工作灯

 public native int offWorkLed();//关工作灯

 

import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;

public class MainActivity extends Activity {
	private static final String TAG = "MainActivity";
	// boolean battery = false;
	private final int CLOSE_ALERTDIALOG = 0;
	private int timecount = 0;
	private int batteryValue = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);

		registerReceiver(batteryChangedReceiver, new IntentFilter(
				Intent.ACTION_BATTERY_CHANGED));
		delayCloseController.timer.schedule(delayCloseController, 1500, 1000);

	}

	private BroadcastReceiver batteryChangedReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {

			if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
				int level = intent.getIntExtra("level", 0);
				int scale = intent.getIntExtra("scale", 100);
				Log.d(TAG, "----------level---" + level);
				Log.d(TAG, "----------scale---" + scale);
				Log.d(TAG, "----------Total---" + level * 100 / scale + "%");
				batteryValue = level * 100 / scale;
			}
		}

	};

	@Override
	protected void onDestroy() {
		MainActivity.this.unregisterReceiver(batteryChangedReceiver);
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case CLOSE_ALERTDIALOG: {
				if (timecount % 2 == 1) {
					if (batteryValue <= 15) {
						onPowerLed();
						try {
							Thread.sleep(300);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						offPowerLed();
						Log.d(TAG, "------PowerLed------");
					}
				} else {
					onWorkLed();
					try {
						Thread.sleep(300);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					offWorkLed();
					Log.d(TAG, "------WorkLed------");
				}
			}
				timecount++;
				break;
			default:
				break;
			}
		}
	};

	private class DelayCloseController extends TimerTask {
		private Timer timer = new Timer();

		@Override
		public void run() {
			Message messageFinish = new Message();
			messageFinish.what = CLOSE_ALERTDIALOG;
			mHandler.sendMessage(messageFinish);
		}
	}

	private DelayCloseController delayCloseController = new DelayCloseController();

	static {
		System.loadLibrary("ledctl-jni");
	}

	public native int onPowerLed();

	public native int offPowerLed();

	public native int onWorkLed();

	public native int offWorkLed();

}

 

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;

public class MainActivity extends Activity {
	private static final String TAG = "MainActivity";
	//boolean battery = false;
	private final int CLOSE_ALERTDIALOG = 0;
	private int timecount = 0;
	private int batteryValue = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);

	new Thread() {
			public void run() {
				registerReceiver(batteryChangedReceiver, new IntentFilter(
						Intent.ACTION_BATTERY_CHANGED));
			}
		}.start();

		new Thread() {
			public void run() {
				setWorkLed();
			}
		}.start();
		
		

	}

	private BroadcastReceiver batteryChangedReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {

			if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
				int level = intent.getIntExtra("level", 0);
				int scale = intent.getIntExtra("scale", 100);
				Log.d(TAG, "----------level---" + level);
				Log.d(TAG, "----------scale---" + scale);
				Log.d(TAG, "----------Total---" + level * 100 / scale + "%");
				
				setPowerLed(level * 100 / scale);
			}
		}

	};

	@Override
	protected void onDestroy() {
		MainActivity.this.unregisterReceiver(batteryChangedReceiver);
	}

	private void setWorkLed() {
		while (true) {
			try {
				onWorkLed();
				Thread.sleep(300);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			try {
				offWorkLed();
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

	private void setPowerLed(int batteryValue) {
		if (batteryValue <= 20) {
			while (true) {
				try {
					onPowerLed();
					Thread.sleep(300);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				try {
					offPowerLed();
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	

	
	static {
		System.loadLibrary("ledctl-jni");
	}

	public native int onPowerLed();

	public native int offPowerLed();

	public native int onWorkLed();

	public native int offWorkLed();

}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值