【问题标题】:Flashligth Android app - feature not working on phones手电筒 Android 应用程序 - 功能无法在手机上运行
【发布时间】:2015-05-22 14:08:34
【问题描述】:

我是一名刚开始使用 Android 的开发人员。 我开发了一款适用于 Android 的应用,可在平板电脑和手机上运行,​​提供包括手电筒在内的各种服务。

我在清单中添加了这个配置:

<Uses-feature android: name = "android.hardware.camera" android: required = "false" />
<Uses-feature android: name = "android.hardware.telephony" android: required = "false" />

我认为通过这最后两个可选功能,我可以在不排除平板电脑的情况下访问手机上的闪存设备,以便安装该应用程序,但它不起作用。 我哪里错了?有人可以帮帮我吗?

【问题讨论】:

  • 它到底是怎么不工作的?另外,请考虑 android.stackexchange.com

标签: android permissions


【解决方案1】:

上面的代码只是给了闪光灯的权限。

您必须编写代码来打开和关闭 Flash

我正在添加我为打开和关闭闪光灯所做的演示的 sn-p。

MainActivity.java

package com.example.balaji.myapplication;

import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {


    private Camera cam;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button turnOn = (Button)findViewById(R.id.turnOnFlashLight);
        turnOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                turnOnFlashlight();
            }
        });

        Button turnOff = (Button)findViewById(R.id.turnOffFlashLight);
        turnOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                turnOffFlashlight();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void turnOnFlashlight(){
        cam = Camera.open();
        Camera.Parameters p = cam.getParameters();
        p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        cam.setParameters(p);
        cam.startPreview();
    }

    public void turnOffFlashlight(){
        cam.release();
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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=".MainActivity">


    <Button
        android:id="@+id/turnOnFlashLight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Turn On"

        />

    <Button
        android:id="@+id/turnOffFlashLight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Turn Off"

        />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.balaji.myapplication" >


    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我还提供了 stackoverflow 链接以获取更多信息。

How to turn on FlashLight in Lollipop programmatically Android

【讨论】:

  • 如果答案有帮助,请接受答案并为答案投票。
猜你喜欢
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多