android调用jni操作指示灯
分享一下自己的笔记, 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(); }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28