Launching an external android app in C#/Unity - java

So i've been stuck on this problem for literally days. I have integrated Unity with Eclipse IDE and i can build and deploy projects perfectly. However, im trying to start an a basic Intent on the java side and trigger it on the Unity/C# side.
Here's my code for the Java side:
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import com.unity3d.player.UnityPlayerNativeActivity;
public class AppLauncher extends UnityPlayerNativeActivity
{
public Intent myIntent;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Assuming that we want to launch the browser to browse a website
Uri uri = Uri.parse("http://www.google.com");
myIntent= new Intent(Intent.ACTION_VIEW, uri);
}
public void Trigger()
{
startActivity(myIntent);
}
}
Here's the error im getting thrown by logcat when the C# trigger is hit:
And here's my code for the C# side of if it:
if(s[0].Equals("Spr"))
{
print("Launched");
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("Trigger");
}
Here's the error im getting thrown by logcat when the C# trigger is hit:
AndroidJavaException: java.lang.NoSuchMethodError: no method with name='Trigger' signature='()V' in class Lcom/unity3d/player/UnityPlayerNativeActivity;
I've tried screwing around by passong a custom signature along with the Trigger method name in the C# script, ive tried extending the standard UnityPlayerActivity, etc... I've tried hours worth of stuff and i Cannot seem to solve tis problem.
Any help is greatly accepted!

Are you sure that your activity is created? Do you really need to extend the activity? I'd try something like this:
Java:
package my.package;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class AppLauncher
{
public static void Trigger()
{
//Assuming that we want to launch the browser to browse a website
Uri uri = Uri.parse("http://www.google.com");
Intent myIntent= new Intent(Intent.ACTION_VIEW, uri);
startActivity(myIntent);
}
}
C#:
AndroidJavaObject jo = new AndroidJavaObject("my.package.AppLauncher");
jo.CallStatic("Trigger");

Related

Broadcast Receiver - MainActivity is not an enclosing class

I know this was asked numerous times, but I couldn't find the right answer for me.
I'm only in my third semester of my bachelor degree, so not that much knowledge yet.
Currently having my first Android course after I finished my beginner Java course (emphasis on beginner).
Due to Corona there is next to no Tutor support and the script is lacking decent explanations.
Im struggling with implementing my first Broadcast receiver. It's supposed to make a toast when a power cord is plugged in.
I tried a dynamic receiver so I registered the Receiver in my MainActivity as follows:
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
[...]
private PowerConnectedReceiver mPowerConnectedReceiver;
public void onResume() {
super.onResume();
IntentFilter powerFilter = new IntentFilter(Intent.ACTION_POWER_CONNECTED );
mPowerConnectedReceiver = new PowerConnectedReceiver();
getApplicationContext().registerReceiver(mPowerConnectedReceiver , powerFilter);
}
#Override
protected void onPause() {
getApplicationContext().unregisterReceiver(mPowerConnectedReceiver);
super.onPause();
}
With the Broadcast Receiver looking as follows and this is where the error pops up. It says for the Context of the Toast that MainActivity is not an enclosed class.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import static android.widget.Toast.LENGTH_LONG;
public class PowerConnectedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(MainActivity.this, "POWER CONNECTED received", LENGTH_LONG ).show();
}
}
This is the way of implementing that is taught in the script.
As I mentioned I am fairly new to programming and this might be something totally obvious, but I'm not seeing it.
The only thing I tried was changing it to MainAcivity.class, but that did nothing.
Thanks for support.
Your code is correct, but in the toast message you have to use context of the receiver i.e the first parameter of the onReceive method.
Update your toast message from:
Toast.makeText(MainActivity.this, "POWER CONNECTED received", LENGTH_LONG ).show();
to
Toast.makeText(context, "POWER CONNECTED received", LENGTH_LONG ).show();

How to create service Detaction call always running after killed app over api 26 in android studio java

i am using this code
and this code running if app active in screen
and if closed the code is not running
how to fix this like truecaller app is running in background if killed app
package com.islamelwakeel.detectcall;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
showToast(context,"Call started...");
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)){
showToast(context,"Call ended...");
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)){
showToast(context,"Incoming call...");
Log.d("call","Incoming call...");
}
}
void showToast(Context context,String message){
Toast toast=Toast.makeText(context,message,Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
}
My service was also killed. I tried many methods, but I discovered one method that worked. I used a service that receives calls from the system to check if my service is still running. If it doesn't work, start it. New Service. For example, NotificationListenerService will have it. There will be some events all the time. And I can write code to restart my service there
Ex.
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
if(!isMyserviceRunning){
startService();
}
}

How to make foreground Services work in MIUI?

I've been messing around with android services and facing a problem while running foreground Services in MIUI 10(Testing Device: Redmi note 5 pro)
Basically Service runs as long as the user is interacting with activity but as soon as user kills activity, foreground service also gets killed.
I read some other answers regarding the same issues,
which state that in devices like Xaomi, Oppo, lenovo, LG, honor etc.
You need to enable "AutoRun" permission for the app
Which I tried with no success. I also tried the following with no success at all:
Disabled MIUI optimization
Disabled Power saving
Removed Battery restrictions for the app
Freed the memory (Total: 3GB, Available:
2GB)
What worked for me was enabling the: "Don't keep activites" in the Developer options but in real world application you probably wouldn't ask users to enable this option since it affects user experience.
By the way, I tested my app in other devices such as pixel, nexus etc(Android studio emulators) And They all worked fine. It's only my device which is causing this issue.
Download link for the app for debugging purposes: https://anonfile.com/d4k511p1bd/app-debug_apk
Source Code
File: MainActivity.java
package com.myname.foregroundserviceexample;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText editTextInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextInput = findViewById(R.id.edit_text_input);
}
public void startService(View v) {
String input = editTextInput.getText().toString();
Intent serviceIntent = new Intent(this, ExampleService.class);
serviceIntent.putExtra("inputExtra", input);
ContextCompat.startForegroundService(this, serviceIntent);
}
public void stopService(View v) {
Intent serviceIntent = new Intent(this, ExampleService.class);
stopService(serviceIntent);
}
}
File: ExampleService.java
package com.myname.foregroundserviceexample;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import static com.myname.foregroundserviceexample.App.CHANNEL_ID;
public class ExampleService extends Service {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
String input = intent.getStringExtra("inputExtra");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Example Service")
.setContentText(input)
.setSmallIcon(R.drawable.ic_android)
.setContentIntent(pendingIntent)
.build();
// Starting Foreground Service
startForeground(1, notification);
return START_NOT_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
File: App.java
package com.myname.foregroundserviceexample;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
public class App extends Application {
public static final String CHANNEL_ID = "exampleServiceChannel";
#Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Example Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
}
I know there's a way around this by enabling "Don't keep activities" in Developing options but I genuinely don't want the user to enable this in their device, Also I would gladly accept any alternative or code improvements to make the foreground Service work in MIUI 10.
Thankyou
EDIT:
Here's the project link: https://anonfile.com/y5Rd4bp3b9/ForegroundServiceExample_zip
And this is the tutorial I was following on YouTube: https://www.youtube.com/watch?v=FbpD5RZtbCc
Maybe you should try to call Service#startForeground at the begining of your service creation in the onCreate/onStartIntent
See this post Context.startForegroundService() did not then call Service.startForeground() for more information.
If you are facing an error like this.
Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE
Then you have to add this in your manifest file.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"></uses-permission>
Hope it's work for you.

Programatic access to Home button in React Native [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I'm trying to programatically call Home button with another button inside my project. App compiles fine, but when I tap the button that should call Home I receive following error:
Attempt to invoke virtual method 'void
android.content.Context.startActivity(android.content.Intent)' on a
null object reference
Here is my code (just essentials):
import android.app.Activity;
import android.content.Intent;
import android.content.Context;
public class ClassName extends ReactContextBaseJavaModule {
private Context context;
#ReactMethod
public void minApp() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMain);
}
}
What am I doing wrong?
SOLUTION:
Due to the fact that my app uses react native, the code in bridged method in java file should look as below:
import android.content.Intent;
import android.content.Context;
public class ClassName extends ReactContextBaseJavaModule {
#ReactMethod
public void minApp() {
Context context = getReactApplicationContext();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMain);
}
}
Using this we can assign Home button function anywhere we want ;)
public void openLauncher(Context context) {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMain);
}
You can just open the launcher using this function.
Seems like you haven't initialized context. Make sure to initiate it before calling minApp()

android studio media player null object reference

I ran into trouble when trying to use the media player in Android Studio, it seems like i haven't initialize the media player properly, which caused the app to crash (on phones running on Android 8.0) when SoundFXPlayer.setVolume() function is called. However, the app worked properly in emulator (API 26 - 28) , and most of the phone i tested (phones with on API 26-27) , EXCEPT phones that run on Android 8.0 (API 28) .
To summarize
1. How to initialize media player properly
2. Why my code work on emulator but not on phone.
i am new to stackoverflow and not experienced in programming, sorry if didn't gave enough details.
Error message as follow:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.garmischwong.buttongame/com.example.garmischwong.buttongame.MenuActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.media.MediaPlayer.setVolume(float, float)' on a null object
reference
package com.example.garmischwong.buttongame;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MenuActivity extends AppCompatActivity {
private Button gameButton;
public int SoundFXVolume;
public MediaPlayer SoundFXPlayer;
public static final String GAME_PREF = "gamePref" ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
gameButton = findViewById(R.id.gameButton);
SoundFXPlayer = MediaPlayer.create(this, R.raw.menu_selection_click);
loadSoundFX();
SoundFXPlayer.setVolume((float)SoundFXVolume/100,(float)SoundFXVolume/100);
}
public void goToGameAct(View view)
{
//////Intent to game///////
SoundFXPlayer.start();
Intent goToGameIntent = new Intent(MenuActivity.this, GameActivity.class);
startActivity(goToGameIntent);
finish();
}
public void loadSoundFX()
{
SharedPreferences gamePref = getSharedPreferences(String.valueOf(GAME_PREF), MODE_PRIVATE);
SoundFXVolume = gamePref.getInt("Sound_FX_Volume", 30);
}
Most likely audio format is not being supported. You can use 8bit and 16bit linear PCM. Here is more information on supported media formats:supported media formats
The solution is to re-encode the mp3 files.

Categories

Resources