我的手电筒应用程序适用于S2 S3和其他几款Android手机没有接近先进的S1但由于某些原因无法打开S1s闪光灯这个问题已经报道在机器人X和其他几个机器人任何熟悉这些兼容的人。代码如下
public class FlashLightActivity extends Activity {
private Camera mCamera;
private Boolean mFlashlight = false;
private Boolean sound = true;
private ImageButton button;
private static MediaPlayer mediaPlayer;
boolean flashInstalled = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flashlight);
AdView ad = (AdView) findViewById(R.id.adView);
ad.loadAd(new AdRequest());
button = (ImageButton) findViewById(R.id.onoff);
mediaPlayer = new MediaPlayer();
Context context = this;
PackageManager pm = context.getPackageManager();
// if device support camera?
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
Toast.makeText(this, "Your device does not have FlashLight",
Toast.LENGTH_LONG).show();
return;
}
try {
// Checking if flashplayer is available
ApplicationInfo ai = pm.getApplicationInfo("com.adobe.flashplayer",
0);
if (ai != null)
flashInstalled = true;
} catch (NameNotFoundException e) {
flashInstalled = false;
}
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mCamera != null) {
Parameters params = mCamera.getParameters();
if (mFlashlight) {
params.setFlashMode(Parameters.FLASH_MODE_OFF);
button.setImageResource(R.drawable.off_icon);
mFlashlight = false;
} else {
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
button.setImageResource(R.drawable.on_icon);
mediaPlayer = MediaPlayer.create(
FlashLightActivity.this, R.raw.boom);
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
if (sound) {
mediaPlayer.stop();
mediaPlayer = null;
} else {
mediaPlayer.start();
}
v.setKeepScreenOn(true);
mFlashlight = true;
}
mCamera.setParameters(params);
}
}
});
}
@Override
public void onResume() {
super.onResume();
SharedPreferences preferences = getSharedPreferences("pref",
MODE_PRIVATE);
sound = preferences.getBoolean("sound", false);
if (mFlashlight) {
button.setImageResource(R.drawable.on_icon);
} else {
button.setImageResource(R.drawable.off_icon);
}
try {
this.mCamera = Camera.open();
} catch (Exception e) {
}
}
@Override
public void onPause() {
super.onPause();
if (this.mCamera != null) {
this.mCamera.release();
this.mCamera = null;
}
this.mFlashlight = false;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_tabs, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Intent menuIntent = new Intent(this, MenuActivity.class);
startActivity(menuIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
答案 0 :(得分:0)
我发现在Droid X上工作的唯一一件事就是Siddhpura Amit在这个答案的下方部分提供的代码:Use camera flashlight in Android。他检查制造商并检查它是否包含字符串“motorola”。如果是这样,他有特殊的代码可以打开或关闭相机闪光灯LED。我可以验证它是否有效,因为我有一个摩托罗拉Droid X,这是我在我的应用程序中使用的代码。