Skip to content

Instantly share code, notes, and snippets.

@sunnymopada
Created May 10, 2021 14:39
Show Gist options
  • Save sunnymopada/04b896dd1ae3b09411ba052e984e74a2 to your computer and use it in GitHub Desktop.
Save sunnymopada/04b896dd1ae3b09411ba052e984e74a2 to your computer and use it in GitHub Desktop.
Android Snippets
String toEmail = "";
String subject = "Autofill subject";
String description = "Autofill body";
String emailChooserHeading = "Email via...";
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL , new String[] { toEmail });
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, description);
startActivity(Intent.createChooser(intent, emailChooserHeading));
package com.proyuga.testQRScanner;
import com.unity3d.player.*;
import android.app.Activity;
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.OnLifecycleEvent;
import android.arch.lifecycle.ProcessLifecycleOwner;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class UnityPlayerActivity extends Activity implements LifecycleObserver {
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
// Global variables in UnityPlayerActivity activity
private Handler handler = new Handler();
private Runnable runnable;
private int TIME_TO_KILL_APP = 5 * 60 * 1000; // 5 minuits in milli seconds
Button okButton;
// Setup activity layout
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
mUnityPlayer = new UnityPlayer(this);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
setupLifeCycleListener();
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
protected void onNewIntent(Intent intent) {
// To support deep linking, we need to make sure that the client can get access to
// the last sent intent. The clients access this through a JNI api that allows them
// to get the intent set on launch. To update that after launch we have to manually
// replace the intent with the one caught here.
setIntent(intent);
}
private void setupLifeCycleListener() {
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onMoveToForeground() {
Log.d("OnLifecycleEvent", "onStart");
// To remove handler if user comes to app with in TIME_TO_KILL_APP time
if (runnable != null) {
handler.removeCallbacks(runnable);
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStopMethod() {
Log.d("OnLifecycleEvent", "onStop");
runnable = new Runnable() {
@Override
public void run() {
Log.d("UnityPlayerActivity", "Killing the app");
finishAffinity();
}
};
handler.postDelayed(runnable, TIME_TO_KILL_APP);
}
// Quit Unity
@Override
protected void onDestroy() {
Log.d("SampleLifecycle", "onDestroy");
if (runnable != null) {
handler.removeCallbacks(runnable);
}
mUnityPlayer.quit();
super.onDestroy();
}
// Pause Unity
@Override
protected void onPause() {
super.onPause();
mUnityPlayer.pause();
}
// Resume Unity
@Override
protected void onResume() {
super.onResume();
mUnityPlayer.resume();
}
@Override
protected void onStart() {
super.onStart();
mUnityPlayer.start();
}
@Override
protected void onStop() {
super.onStop();
mUnityPlayer.stop();
}
// Low Memory Unity
@Override
public void onLowMemory() {
super.onLowMemory();
mUnityPlayer.lowMemory();
}
// Trim Memory Unity
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (level == TRIM_MEMORY_RUNNING_CRITICAL) {
mUnityPlayer.lowMemory();
}
}
// This ensures the layout will be correct.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
// Notify Unity of the focus change.
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
// For some reason the multiple keyevent type is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
return mUnityPlayer.injectEvent(event);
return super.dispatchKeyEvent(event);
}
// Pass any events not handled by (unfocused) views straight to UnityPlayer
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
return mUnityPlayer.injectEvent(event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return mUnityPlayer.injectEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mUnityPlayer.injectEvent(event);
}
/*API12*/
public boolean onGenericMotionEvent(MotionEvent event) {
return mUnityPlayer.injectEvent(event);
}
}
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
//for Android 5-7
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);
// for Android O
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
startActivity(intent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment