0

I need to start an Activity in case of device shake in my Android application.

This feature should work in my whole app and in all activities.

Now sure how to provide a good architecture for it, because the detecting shake and starting an Activity needs context instance.

I don't want to pass my current Activity instance to the my ShakeListener each time I change the activity.

What will you suggest?

Thanks.

Sumit Munot
  • 3,748
  • 1
  • 32
  • 51
narek.gevorgyan
  • 4,165
  • 5
  • 32
  • 52

1 Answers1

1

You would certainly have to keep around something that monitors sensor data, like a background Service. You service must continuously monitor sensor events, pass it through a noise filter, detect a shake and send out a Broadcast.

The Activity on the other hand can implement a matching BroadcastReciever.

If you don't want to add BroadcastRecievers for Activities, then you can just register interested activities in init() code of library:

public void init(Context context){ 
    SensorTester st = new SensorTester(context);

    st.registerActivity(MainActivity.class);
    st.registerActivity(OtherActivity.class);
}

And start them from library service using the Context you received in init() code:

for(Class c : registeredClasses){
     context.startActivity(new Intent(this,c));
}

Where you want to place init() code in target App is your choice.

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • Basically, I'm writing an Android library which will be used for testers. Tester just adds the library to Android app and calls init code. I want to make the changes in the Base application code minimal, so creating broadcast recevier will require changes in Manifest file, of which I'm trying to avoid. Hope this is clear. – narek.gevorgyan May 21 '13 at 12:34
  • Can I catch the shake event and start an activity within Application class extending it? – narek.gevorgyan May 21 '13 at 12:35
  • @narek.gevorgyan Updated answer. `Application` class would be around, only when at least one child component (Activity, Service etc) is running. – S.D. May 21 '13 at 12:50
  • Thanks 1. The context is an Application instance right? 2. Should the MainActivity and OtherAcitivy be registered in the Base application Manifest? – narek.gevorgyan May 21 '13 at 13:00
  • @narek.gevorgyan 1. depends where you call `init()`, may be in application class or in the main activity. 2. Library/base manifest doesn't need any thing special except the service declaration. And target App will of course have entries in manifest for each of its Activities like a normal project. – S.D. May 21 '13 at 13:06
  • 1. Ok 2. I need to start activities which are in my Library project. And I dont want to add them into base app manifest. – narek.gevorgyan May 21 '13 at 13:19
  • The idea is when tester finds a bug he simply shakes a device and it brings the report screen. – narek.gevorgyan May 21 '13 at 13:19
  • @narek.gevorgyan oh, then yes of course declare that reporting activity in library and its manifest. And in this case you won't need to register target app's Activites (library got its own reporting activity), in `init()` just save the client app's context and start your service with it. – S.D. May 21 '13 at 13:23
  • It says that these activities are not declared in Manifest. I suppose I should declare them in the base app manifest, but I dont what to do it. It there a turnaround here? – narek.gevorgyan May 21 '13 at 13:29