Shake device to launch application

I use this to work with Shake, and it works fine for me, but I want to run the application when the user shakes his device, see my code below:

@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); } 

So here is my question: how can I launch the application by shaking my device?

+6
source share
4 answers

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); } } 
+3
source

Write a standalone application for Shake detection . If jitter is detected, miss the intent with the package name of the application you want to run:

 Intent intent = new Intent (<PackageNameOfAppToBeLaunched>); startActivity (intent); 
+2
source

Well, what you need is two different kinds of activity. Where is the first One Detects your Shake, which you need to run all the time in the background, and call the new actual application that you want to start when shaking.

You can run a background action that you must use to keep it running in the background for a long time. (Continuous). You can use Like FutureTask or Executor classes (you cannot > use AsyncTask for this).

Whenever a thread passes a command to your application to open after Shake, the Background process stops and the command goes to the application, which means you need to start the background process again immediately after closing the application.

+2
source

You need to write code to launch the application to the foreground from the background during the start of the shake. This link will help you do this.

+1
source

Source: https://habr.com/ru/post/886789/


All Articles