我想制作类似三星s5附带的手电筒应用程序。因此,如果您按下应用程序图标,它不会打开屏幕上的任何窗口,它只会启用手机上的手电筒并再次按下会像切换一样关闭手电筒。我目前有一个工作的手电筒应用程序,但它在屏幕上打开一个窗口,我必须按下窗口上的按钮打开手电筒。我想消除窗口,只需按下应用程序图标即可打开/关闭手电筒。
我在Android Studio上用java编程。我对Android应用程序的编程非常新,所以请随时详细介绍。谢谢!
到目前为止我得到了什么......
主要java代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flashlight_demo);
this.setContentView(R.layout.activity_flashlight_demo);
this.flashBtn = (Button) this.findViewById(R.id.btnswitch);
hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
// device doesn't support flash
// Show alert message and close the application
AlertDialog alert = new AlertDialog.Builder(FlashlightDemo.this)
.create();
alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish();
}
});
alert.show();
return;
}
getCamera();
flashBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFlashOn) {
// turn off flash
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
}
布局xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_flashlight_demo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.flashlightdemo.magikarp.flashlightdemo.FlashlightDemo">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<Button
android:id="@+id/btnswitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="118dp"
android:layout_y="95dp"
android:background="@drawable/finalspark"
android:onClick="toast"
android:text=" "
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.flashlightdemo.magikarp.flashlightdemo">
<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=".FlashlightDemo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>