当我构建项目时,我选择了android 4.0.3 IceCreamSandwich。之后我安装在android os.6和android os.4手机上。我意识到只适用于操作系统4,较新的API不能支持旧的API?这听起来不是逻辑.........
Andoird表现
rn = 2
Android Main
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jackd.testflash_26_4_2017">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Android XML布局
package com.example.jackd.testflash_26_4_2017;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Camera camera;
private Camera.Parameters params;
Button btnFlash;
TextView txtArea,txtArea2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnFlash = (Button) findViewById(R.id.btnFlash);
final String myString = "110101010101";
final long blinkDelay = 50;
btnFlash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0;i<myString.length();i++){
if(myString.charAt(i) == '0'){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
}
else{
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
}
try{
Thread.sleep(blinkDelay);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
});
if (isFlashSupported()) {
camera = Camera.open();
params = camera.getParameters();
} else {
showNoFlashAlert();
}
}
private void showNoFlashAlert() {
new AlertDialog.Builder(this)
.setMessage("Your device hardware does not support flashlight!")
.setIcon(android.R.drawable.ic_dialog_alert).setTitle("Error")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
}).show();
}
private boolean isFlashSupported() {
PackageManager pm = getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
@Override
protected void onDestroy() {
if(camera != null){
camera.stopPreview();
camera.release();
camera = null;
}
super.onDestroy();
}
}
PS:我已经打开了相机硬件的所有权限。在OS6中,它需要手动打开,我已经做到了!
回到问题,Version6 Marshmallow android不能支持version4 iceCreamSandwith吗?但是,逻辑上更高的版本应该支持更低版本....感谢您的帮助!
答案 0 :(得分:0)
android开发者:
从Android 6.0(API级别23)开始,用户授予权限 应用程序运行时的应用程序,而非安装应用程序时的应用程序。
您可以查看此信息以获取更多信息:
https://developer.android.com/training/permissions/requesting.html