Андроид. Intent.КНОПКА АКЦИОНЕРНОЙ КАМЕРЫ не работает

Изучение широковещательных сообщений. В книге Д.Н.Колисниченко - "Программирование для Android" есть пример, где камера по нажатию кнопки запускает службу. Но у меня он че то не работает. В чем проблема? Либо я не знаю, что за кнопка камеры (физическая или на дисплее), либо не понимаю, что не так. Вот код примера:

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.content.IntentFilter;

public class MainActivity extends AppCompatActivity {


    MyReceiver iReceiver = new MyReceiver();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter iFilter =  new IntentFilter(Intent.ACTION_CAMERA_BUTTON);

        iFilter.addAction(Intent.ACTION_CAMERA_BUTTON);

        registerReceiver(iReceiver, iFilter);
    }
    @Override
    protected void onDestroy() {
        unregisterReceiver(iReceiver);
        super.onDestroy();
    }
}

MyReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context rcvContext, Intent rcvIntent) {
        String action = rcvIntent.getAction();

        if (action.equals(Intent.ACTION_CAMERA_BUTTON)) {

            rcvContext.startService(new Intent(rcvContext,
                    MyService.class));
        }
    }
}

MyService.java

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this,"Service started...",
                Toast.LENGTH_LONG).show();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service destroyed...",
                Toast.LENGTH_LONG).show();
    }
}

И манифест.xml

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

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <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" />
                <action android:name="android.intent.action.CAMERA_BUTTON" />
            </intent-filter>
        </activity>

        <receiver android:name=".MyReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.CAMERA_BUTTON" />
            </intent-filter>
            <service android:name=".MyService"></service>
            <action android:name="android.intent.action.CAMERA_BUTTON" />
            <action android:name="android.intent.extra.KEY_EVENT" />
            <uses-permission android:name="android.permission.CAMERA" />
            <uses-feature android:name="android.hardware.camera" />
        </receiver>



    </application>

</manifest>

person PeteGr    schedule 15.08.2016    source источник
comment
вы получили разрешение камеры. ‹использует-разрешение android:name=android.permission.CAMERA /›   -  person kggoh    schedule 15.08.2016
comment
Возможный дубликат вещательного приемника для ACTION_CAMERA_BUTTON никогда не вызывается   -  person frogatto    schedule 15.08.2016


Ответы (1)


Убедитесь, что вы добавили разрешение камеры в свой манифест

<uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
person Frank Odoom    schedule 15.08.2016
comment
‹uses-permission android:name=android.permission.CAMERA /› не работает - person PeteGr; 15.08.2016
comment
Попробуйте зарегистрировать разрешения глобально непосредственно перед ‹приложением› - person Frank Odoom; 15.08.2016