You must write an Android service that starts your activity while shaking. That's all. Services run in the background, even if no activity is displayed
A service can be started, for example. during device boot. This can be achieved using BroadCastReceiver.
manifest:
<application ...> <activity android:name=".ActivityThatShouldBeLaunchedAfterShake" /> <service android:name=".ShakeService" /> <receiver android:name=".BootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application>
BootReceiver:
public class BootReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Intent intent = new Intent(context, ShakeService.class); context.startService(intent); } }
Services:
public class ShakeService extends Service { @Override public IBinder onBind(Intent intent) { return null; } ... somewhere if(shaked) { Intent intent = new Intent(getApplicationContext(), ActivityThatShouldBeLaunchedAfterShake.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }
source share