安卓板子开发特殊功能收集

1.系统启动时启动app
分3步完成
1)权限

<!--开机启动权限-->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  1. 自定义广播接收器

//开机自启动广播接受
public class AutoStartBroadcastReceiver extends BroadcastReceiver {
    static final String action_boot ="android.intent.action.BOOT_COMPLETED";
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(action_boot)){
            Intent sayHelloIntent=new Intent(context, MainActivity.class);

            sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            context.startActivity(sayHelloIntent);
        }
    }

}

3)注册该广播接收器

<!-- 开机自启动广播接受 -->
        <receiver android:name=".system.AutoStartBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
  1. 从camera2获取数据帧处理后渲染显示不卡顿
    选用Surfaceview来渲染,据说有双缓冲,在填充bitmap时这样处理:
 SurfaceHolder holder = surfaceView.getHolder();
  Canvas canvas = holder.lockCanvas(new Rect(0,0,width,height));
  //镜像处理
  Matrix matrix = new Matrix();
  matrix.postScale(-1, 1);
  matrix.postTranslate(width,0);
  canvas.drawBitmap(bitmap, matrix, null);
  holder.unlockCanvasAndPost(canvas);

这里顺便利用matrix对其进行了镜像操作

3.多线程处理Bitmap时容易造成c层野指针造成的崩溃
需要加锁,同时我这里用Object存储对象,不知道有木有起到copy效果,貌似出错的概率减少了。

4.app崩溃后自启动


import android.app.Activity;
import android.app.Application;

import java.util.ArrayList;
import java.util.List;

public class MyApplication extends Application {
    List<Activity> list = new ArrayList<>();
    @Override
    public void onCreate() {
        super.onCreate();
        //设置该CrashHandler为程序的默认处理器
        UnCeHandler catchExcep = new UnCeHandler(this);
        Thread.setDefaultUncaughtExceptionHandler(catchExcep);
    }

    /**
     * Activity关闭时,删除Activity列表中的Activity对象*/
    public void removeActivity(Activity a){
        list.remove(a);
    }

    /**
     * 向Activity列表中添加Activity对象
     * 每个activity onCreate时调用此方法
     * 
     * */
    public void addActivity(Activity a){
        list.add(a);
    }

    /**
     * 关闭Activity列表中的所有Activity*/
    public void finishActivity(){
        for (Activity activity : list) {
            if (null != activity) {
                activity.finish();
            }
        }
        //杀死该应用进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}

ps:注意这里的addActivity,一定要调用。如果不调用,killProcess的时候将无法finish他们,可能无法释放某些资源,比如线程未关闭,这样再次启动时会引发各种问题。

/*处理崩溃重叠*/
public class UnCeHandler implements Thread.UncaughtExceptionHandler {
    private Thread.UncaughtExceptionHandler mDefaultHandler;
    public static final String TAG = "CatchExcep";
    MyApplication application;

    public UnCeHandler(MyApplication application){
        //获取系统默认的UncaughtException处理器
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        this.application = application;
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        if(!handleException(ex) && mDefaultHandler != null){
            //如果用户没有处理则让系统默认的异常处理器来处理
            mDefaultHandler.uncaughtException(thread, ex);
        }else{
            try{
                Thread.sleep(2000);
            }catch (InterruptedException e){
                Log.e(TAG, "error : ", e);
            }
            Intent intent = new Intent(application.getApplicationContext(), MainActivity.class);
            PendingIntent restartIntent = PendingIntent.getActivity(
                    application.getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
            //退出程序
            AlarmManager mgr = (AlarmManager)application.getSystemService(Context.ALARM_SERVICE);
            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,
                    restartIntent); // 1秒钟后重启应用
            application.finishActivity();
        }
    }

    /**
     * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
     *
     * @param ex
     * @return true:如果处理了该异常信息;否则没有处理返回false.
     */
    private boolean handleException(Throwable ex) {
        if (ex == null) {
            return false;
        }

        return true;
    }
}

5.关于读写u盘,在开发板上一般都开放了root权限,此时可以用cmd来处理读写

6.安卓串口调试
参考https://blog.csdn.net/qq_32136827/article/details/81129640

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值