0

Im trying to write a simple test app that listens in the background(using BroadcastReceiver) for Google Glass camera events such as taken picture, and than take that picture & do something with it. onReceive() is not being called.

My Google Glass is running XE19.1 OS & my GDK api is Google Glass Development Preview 4.4.2.

My class that extends BroadcastReceiver:

package com.example.recentpicscollage;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.widget.Toast;


public class TestReciever extends BroadcastReceiver {


    //Attempt # 1 listen to picture taken events 
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

         Log.d("DEBUG", "RecentPicsCollage BroadcastReceiver");
         //use the Cursor interface to query data from the Intent ( Intents asynchronous intera & inter app communication)
         Cursor cursor = context.getContentResolver().query(intent.getData(),null,null, null, null);
         //The moveToFirst() method is used to position the cursor pointer at the beginning of the data set
         cursor.moveToFirst();
         //get data under the column _data
         String image_path = cursor.getString(cursor.getColumnIndex("_data"));
         //as toast 
         Toast.makeText(context, "image data : " + image_path, 1000).show();
         //as log 
         Log.d("DEBUG", "image data: "+image_path);
         for (int i=0; i<cursor.getColumnNames().length; i++)
                 {
             Log.d("DEBUG", "column name: "+cursor.getColumnNames()[i]);
                 }
         //just print out all data to see what data  is available 
         while (cursor.isAfterLast()!=true)
         {
             //move to next row
            cursor.moveToNext();
            //get data 
            String data = cursor.getString(cursor.getColumnIndex("_data"));
             Log.d("DEBUG", "image data: "+data);

         }

    }

    /*
    //Attempt # 2 , Test to see if i can capture any other action such as TAP 

    @Override
    public void onReceive(Context context, Intent intent) {


            System.out.println("onReceive TAP");
            Log.d("DEBUG", "onReceive TAP");


    }
    */

}//end class

AndroidManifest file:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <!-- permission to read SD card -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.example.recentpicscollage.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>

        <!-- broadcast receiver that listens actions/event associated with NEW PICTURE -->
        <receiver
            android:name=".TestReciever"
            android:enabled="true"
             android:priority="999" >
            <intent-filter>
                <action android:name="com.android.camera.NEW_PICTURE" />
                <action android:name="android.hardware.action.NEW_PICTURE" />
                <action android:name="android.provider.MediaStore.ACTION_IMAGE_CAPTURE" />
                <action android:name="com.google.android.glass.media.CameraManager.ACTION_IMAGE_CAPTURE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>
        </receiver>

<!--  broadcast receiver that listens actions/event associated with a TAP-->
<!--  
   <receiver android:name=".TestReciever" android:exported="false">
    <intent-filter android:priority="999" >
       <action android:name="com.google.glass.action.TAP" />

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

    </application>

</manifest>

In my first attempt a I tried to capture image capture event & I tried all combinations of filter such as:

  <action android:name="com.android.camera.NEW_PICTURE" />
                    <action android:name="android.hardware.action.NEW_PICTURE" />
                    <action android:name="android.provider.MediaStore.ACTION_IMAGE_CAPTURE" />
                    <action android:name="com.google.android.glass.media.CameraManager.ACTION_IMAGE_CAPTURE" />
                    <category android:name="android.intent.category.DEFAULT" />

But nothing is being logged or printed to the console.

In my 2nd attempt I tried registering for another event in this case TAP event , but I cant get onReceive() to be called for that either, the code is simple I'm either missing something very small or their is a bug in XE19.1 release for Glass.

Any intellegent help will be appreciated.

Thanks

cyber101
  • 2,822
  • 14
  • 50
  • 93

1 Answers1

0

I don´t know if this is part of your problem... I´m still learning to use the Glasses, but in your manifest you are missing

uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT"

Also, are you registering the reciever in the Main class? For example, I'm using in an app the bluetooth and the Broadcastreviever I register it at onCreate() like

Filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);    // Register the BroadcastReceiver
registerReceiver(mReceiver, Filter);                        // Don't forget to unregister during onDestroy

Where mReceiver is in my case is

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {   // Create a BroadcastReceiver for ACTION_FOUND
    public void onReceive(Context context, Intent intent) {
    // Methods...
    }
}

Hope this helps!

gkapellmann
  • 313
  • 3
  • 19
  • @I added the DEVELOPMENT permission although it is required by only some APIs such as voice recognition, and still not working. You don't need to declare BroadcatsReceiver programatically you can declare it in the manifest and pass the name of the class that inherits from BroadcastReceiver class. Your approach might work if I was to launch the main activity manually at least one, which will register the BroadcastReceiver in onCreate(). – cyber101 Aug 25 '14 at 05:55
  • 1
    Right! So you are trying to run this reciever even when there is another app present (As you say Background)? Well in this [link](http://stackoverflow.com/questions/12274997/why-broadcastreceiver-works-even-when-app-is-in-background) someone is trying to not let his reciever run all the time, oposite from you, might help! – gkapellmann Aug 25 '14 at 09:39