When i am calling "private class Engine extends CanvasWatchFaceService.Engine", i'm getting the error of 'No enclosing instance of type 'android.support.wearable.watchface.CanvasWatchFaceService' is in scope'
I have called imported the class import android.support.wearable.watchface.CanvasWatchFaceService; but it says it's a unused import statement
UPDATED: This is the whole of my MyWatchFace.java
package com.projects.kainowitzke.googlewatchface;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.wearable.watchface.CanvasWatchFaceService;
import android.support.wearable.watchface.WatchFaceService;
import android.view.SurfaceHolder;
import android.support.wearable.watchface.CanvasWatchFaceService;
public class MyWatchface {
}
public class AnalogWatchFaceService extends MyWatchface {
#Override
public WatchFaceService.Engine onCreateEngine() {
/* provide your watch face implementation */
return new CanvasWatchFaceService.Engine();
}
/* implement service callback methods */
private class MyWatchfac extends CanvasWatchFaceService.Engine {
#Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
/* initialize your watch face */
}
#Override
public void onPropertiesChanged(Bundle properties) {
super.onPropertiesChanged(properties);
/* get device features (burn-in, low-bit ambient) */
}
#Override
public void onTimeTick() {
super.onTimeTick();
/* the time changed */
}
#Override
public void onAmbientModeChanged(boolean inAmbientMode) {
super.onAmbientModeChanged(inAmbientMode);
/* the wearable switched between modes */
}
#Override
public void onDraw(Canvas canvas, Rect bounds) {
/* draw your watch face */
}
#Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
/* the watch face became visible or invisible */
}
}
}
MyWatchface must extend CanvasWatchFaceService.
Related
import android.app.Application;
import com.estimote.coresdk.observation.region.Region;
import com.estimote.coresdk.service.BeaconManager;
import java.util.UUID;
public class MyApplication extends Application {
private BeaconManager beaconManager;
#Override
public void onCreate(){
super.onCreate();
beaconManager = new BeaconManager(getApplicationContext());
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
#Override
// 비콘 uuid,major minor
public void onServiceReady() {
beaconManager.startMonitoring(new Region());
}
});
}
}
this is my code
when I use this code
-beaconManager.startMonitoring(new Region());-
get error named cannot be instanted
how can I fixed??
http://loveiskey.tistory.com/207
this is the site where I referd
Estimote Region is an interface. You cannot instantiate interfaces. You need to instantiate some class that implements that interface instead.
When you start a project with libgdx it automatically makes the class extend Android Application. I did not think about this until later and now I want to change it to the Game and Screen classes. But unfortunately without success...
My first question is, how to I change the android project?
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
public class AndroidClass extends AndroidApplication {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new SplashScreen(), cfg);
}
}
My second question: How do I change the deskop project:
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
public class DeskopClass {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "MyApp";
cfg.useGL20 = false;
cfg.width = 800;
cfg.height = 480;
new LwjglApplication(new SplashScreen(), cfg);
}
}
Third question: How do I change the SplashScreen:
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class SplashScreen implements ApplicationListener{
#Override
public void create() {
// TODO Auto-generated method stub
}
Thanks!
The Game class is just an ApplicationListener. AndroidApplication and Game are not interchangeable classes as they accomplish two different things.
You need an AndroidApplication class to pass events on to your ApplicationListener classes. If you want a Game class in your app then you can always create your own.
public class Game implements ApplicationListener {
#Override
public void dispose () {
}
#Override
public void pause () {
}
#Override
public void resume () {
}
#Override
public void render () {
}
#Override
public void resize (int width, int height) {
}
}
Game class in libgdx is itself an ApplicationListener.
You can create a class that extends Game and directly pass its object to initialize (for android) and LwjglApplication (for desktop).
This way you can use setScreen without a problem.
I am confused with unregisterReceiver(). Is it mandatory to unregister a receiver if we have registered it earlier in our code. I have seen that sometime we don't unregister it and still everything works fine but sometime we need to unregister it in order to get rid of run time exception.
Please explain the concept in detail and if possible with example. I have tried reading few books but none of them has explained it so if you know some book which could have a great explanation about it, let me know.
I would really appreciate your efforts.
Thanks.
No, it's not mandatory to unregister your receiver. If you don't unregister it, the receiver continues to receive the Intents it's registered for from Android. You should unregister it when you don't wish to receive any more broadcasts.
this example might help..
better learning with an example
package com.commonsware.android.shaker;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;
public class ShakerDemo extends Activity
implements Shaker.Callback {
private Shaker shaker=null;
private TextView transcript=null;
private ScrollView scroll=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
transcript=(TextView)findViewById(R.id.transcript);
scroll=(ScrollView)findViewById(R.id.scroll);
shaker=new Shaker(this, 1.25d, 500, this);
}
#Override
public void onDestroy() {
super.onDestroy();
shaker.close();
}
public void shakingStarted() {
Log.d("ShakerDemo", "Shaking started!");
transcript.setText(transcript.getText().toString()+"Shaking started\n");
scroll.fullScroll(View.FOCUS_DOWN);
}
public void shakingStopped() {
Log.d("ShakerDemo", "Shaking stopped!");
transcript.setText(transcript.getText().toString()+"Shaking stopped\n");
scroll.fullScroll(View.FOCUS_DOWN);
}
}
package com.commonsware.android.shaker;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.SystemClock;
import java.util.ArrayList;
import java.util.List;
public class Shaker {
private SensorManager mgr=null;
private long lastShakeTimestamp=0;
private double threshold=1.0d;
private long gap=0;
private Shaker.Callback cb=null;
public Shaker(Context ctxt, double threshold, long gap,
Shaker.Callback cb) {
this.threshold=threshold*threshold;
this.threshold=this.threshold
*SensorManager.GRAVITY_EARTH
*SensorManager.GRAVITY_EARTH;
this.gap=gap;
this.cb=cb;
mgr=(SensorManager)ctxt.getSystemService(Context.SENSOR_SERVICE);
mgr.registerListener(listener,
mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
}
public void close() {
mgr.unregisterListener(listener);
}
private void isShaking() {
long now=SystemClock.uptimeMillis();
if (lastShakeTimestamp==0) {
lastShakeTimestamp=now;
if (cb!=null) {
cb.shakingStarted();
}
}
else {
lastShakeTimestamp=now;
}
}
private void isNotShaking() {
long now=SystemClock.uptimeMillis();
if (lastShakeTimestamp>0) {
if (now-lastShakeTimestamp>gap) {
lastShakeTimestamp=0;
if (cb!=null) {
cb.shakingStopped();
}
}
}
}
public interface Callback {
void shakingStarted();
void shakingStopped();
}
private SensorEventListener listener=new SensorEventListener() {
public void onSensorChanged(SensorEvent e) {
if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
double netForce=e.values[0]*e.values[0];
netForce+=e.values[1]*e.values[1];
netForce+=e.values[2]*e.values[2];
if (threshold<netForce) {
isShaking();
}
else {
isNotShaking();
}
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// unused
}
};
}
I wan't to test some open gl stuff in an android instrumentation unit test.
If I'm not mistaken all test are run inside the acual device so I thought opengl calls should work as well.
However this seams to be not the case, or I'm missing something ( so I hope ).
I stated a new project & a very simple test project to evaluete this.
So here are my tests:
package com.example.test;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import android.opengl.GLES20;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import com.example.HelloTesingActivity;
public class AndroidTest extends ActivityInstrumentationTestCase2<HelloTesingActivity> {
public AndroidTest(String pkg, Class<HelloTesingActivity> activityClass) {
super(pkg, activityClass);
}
private HelloTesingActivity mActivity;
public AndroidTest() {
super("com.example", HelloTesingActivity.class);
}
public void testTrue(){
assertTrue(true);
}
#Override
protected void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
}
public void testPreConditions() throws Exception {
assertNotNull(mActivity); // passes
}
/*
* FAILS
*/
#UiThreadTest // ensures this test is run in the main UI thread.
public void testGlCreateTexture(){
IntBuffer buffer = newIntBuffer();
GLES20.glGenTextures(1, buffer);
assertFalse(buffer.get() == 0); // this fails
}
/**
* just a helper to setup a correct buffer for open gl to write the values into
* #return
*/
private IntBuffer newIntBuffer() {
ByteBuffer buff = ByteBuffer.allocateDirect(4);
buff.order(ByteOrder.nativeOrder());
buff.position(0);
return buff.asIntBuffer();
}
/*
* FAILS
*/
#UiThreadTest
public void testGlCalls(){
GLES20.glActiveTexture(1); // set the texture unit to 1 since 0 is the default case
IntBuffer value = newIntBuffer();
GLES20.glGetIntegerv(GLES20.GL_ACTIVE_TEXTURE, value );
assertEquals(1, value.get()); // this fails with expected: 1 but was: 0
}
}
And here is the activity itself, just for the sake of comletness.
package com.example;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class HelloTesingActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GLSurfaceView surface = new GLSurfaceView(this);
surface.setEGLContextClientVersion(2);
setContentView(surface);
surface.setRenderer(new MyRenderer());
}
}
package com.example;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class MyRenderer implements Renderer {
#Override
public void onDrawFrame(GL10 arg0) {
// TODO Auto-generated method stub
}
#Override
public void onSurfaceChanged(GL10 arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
// TODO Auto-generated method stub
}
}
I wanted to test GL code as well, here's how I'm doing it:
import java.util.concurrent.CountDownLatch;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.test.ActivityInstrumentationTestCase2;
/**
* <p>Extend ActivityInstrumentationTestCase2 for testing GL. Subclasses can
* use {#link #runOnGLThread(Runnable)} and {#link #getGL()} to test from the
* GL thread.</p>
*
* <p>Note: assumes a dummy activity, the test overrides the activity view and
* renderer. This framework is intended to test independent GL code.</p>
*
* #author Darrell Anderson
*/
public abstract class GLTestCase<T extends Activity> extends ActivityInstrumentationTestCase2<T> {
private final Object mLock = new Object();
private Activity mActivity = null;
private GLSurfaceView mGLSurfaceView = null;
private GL10 mGL = null;
// ------------------------------------------------------------
// Expose GL context and GL thread.
// ------------------------------------------------------------
public GLSurfaceView getGLSurfaceView() {
return mGLSurfaceView;
}
public GL10 getGL() {
return mGL;
}
/**
* Run on the GL thread. Blocks until finished.
*/
public void runOnGLThreadAndWait(final Runnable runnable) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
mGLSurfaceView.queueEvent(new Runnable() {
public void run() {
runnable.run();
latch.countDown();
}
});
latch.await(); // wait for runnable to finish
}
// ------------------------------------------------------------
// Normal users should not care about code below this point.
// ------------------------------------------------------------
public GLTestCase(String pkg, Class<T> activityClass) {
super(pkg, activityClass);
}
public GLTestCase(Class<T> activityClass) {
super(activityClass);
}
/**
* Dummy renderer, exposes the GL context for {#link #getGL()}.
*/
private class MockRenderer implements GLSurfaceView.Renderer {
#Override
public void onDrawFrame(GL10 gl) {
;
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
;
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
synchronized(mLock) {
mGL = gl;
mLock.notifyAll();
}
}
}
/**
* On the first call, set up the GL context.
*/
#Override
protected void setUp() throws Exception {
super.setUp();
// If the activity hasn't changed since last setUp, assume the
// surface is still there.
final Activity activity = getActivity(); // launches activity
if (activity == mActivity) {
mGLSurfaceView.onResume();
return; // same activity, assume surface is still there
}
// New or different activity, set up for GL.
mActivity = activity;
mGLSurfaceView = new GLSurfaceView(activity);
mGL = null;
// Attach the renderer to the view, and the view to the activity.
mGLSurfaceView.setRenderer(new MockRenderer());
activity.runOnUiThread(new Runnable() {
public void run() {
activity.setContentView(mGLSurfaceView);
}
});
// Wait for the renderer to get the GL context.
synchronized(mLock) {
while (mGL == null) {
mLock.wait();
}
}
}
#Override
protected void tearDown() throws Exception {
mGLSurfaceView.onPause();
super.tearDown();
}
}
You annotated your tests to run on the UIThread but OpenGL calls should be on the GLThread. AFAIK there's no annotation to run these tests.
I am trying to run this Android unit test, following this tutorial ::
http://developer.android.com/resources/tutorials/testing/helloandroid_test.html
and in doing so get a SuperNotCalledException
Here's the test class code ::
package com.example.helloandroid2.test;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;
import com.example.helloandroid2.HelloAndroid2Activity;
public class HelloAndroid2Test extends ActivityInstrumentationTestCase2<HelloAndroid2Activity>
{
private HelloAndroid2Activity mActivity;
private TextView mView;
private String resourceString;
public HelloAndroid2Test()
{
super("com.example.helloandroid2", HelloAndroid2Activity.class);
}
#Override
protected void setUp() throws Exception
{
super.setUp();
mActivity = this.getActivity();
mView = (TextView) mActivity.findViewById(com.example.helloandroid2.R.id.textview);
resourceString = mActivity.getString(com.example.helloandroid2.R.string.hello);
}
public void testPreconditions()
{
assertNotNull(mView);
}
public void testText()
{
assertEquals(resourceString,(String)mView.getText());
}
}
The class I'm actually testing ::
package com.example.helloandroid2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid2Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
}
}
I've set the project API levels at 2_3_1 and am using an avd set at the same.
Am running Eclipse with ADT on Windows Vista.
All wisdom greatfully recieved. Thanks in advance.
Chris
Your onCreate() method in HelloAndroid2Activity needs to call super.onCreate(savedInstanceState);
public class HelloAndroid2Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}