Programatic access to Home button in React Native [duplicate] - java

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()

Related

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

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.

how I can add push notification service to my application?

here I add parse library to my application. but in my main activity i faced an error with parse.initialize .
public void onCreate() {
Parse.initialize(this, "id",
"id");
PushService.setDefaultPushCallback(this, MyActivity.class);
}
You're using initialize in your activity, you should rather try in your Application class, from the docs
The recommended way is to put a call to Parse.initialize in your
Application's onCreate method.
An example:
import android.app.Application;
import com.parse.Parse;
public class MyApplication extends Application {
public void onCreate() {
Parse.initialize(this, "your application id", "your client key");
}
}
you should also add to your manifest
<application
android:name="YourPackageName.MyApplication"
</application>

Launching an external android app in C#/Unity

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");

"localString1" cannot be resolved to a variable and Type mismatch errors [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Please I'm still learning java/android app development and I'm having these errors in my code. Please help.
My code looks like this
package com.creditswitch;
import android.app.Activity;
import android.content.BroadcastReceiver;
import java.io.PrintStream;
import android.content.IntentFilter;
public class MainActivity extends Activity {
BroadcastReceiver mReceiver;
protected void onResume() {
MyApplication.activitycontext = this;
System.out.println("On Resume of base activity");
if(mReceiver == null) {
mReceiver = new ConnectionChangeReceiver();
registerReceiver(mReceiver, localString1); //THIS LINE IS GIVING ERROR: localString1 cannot be resolved to a variable
System.out.println("Register Receiver");
}
super.onResume();
}
protected void onPause() {
System.out.println("On Pause of base activity");
if(mReceiver != null) {
unregisterReceiver(mReceiver);
System.out.println("Unregister Receiver");
mReceiver = 0x0; // THIS LINE IS GIVING ERROR: Type mismatch: cannot convert from int to BroadcastReceiver
}
super.onPause();
}
}
First of all localString1 should be declared of type intentFilter because registerReceiver method accepts two params which are:
1 BraodcastReceiver
2 IntentFilter instance
You can create an intent filter as shown below:
static final String MY_ACTION = "com.my.defined.MY_ACTION";
IntentFilter intentFilter = new IntentFilter(MY_ACTION);
You have to provide the same action in the tag of your androidmanifest.xml, this can be done as shown here.
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="com.my.defined.MY_ACTION" />
</intent-filter>
</receiver>
And Second thing is you have not correctly initialized the mReceiver object.Its of type broadcastReceiver, you can not instantiate it with an integer value
to initialize it you must do the following (or create an anonymous inner class on type BroadcastReceiver):
create a class that extends BroadcastReceiver and override its onreceive method
public class MyBroadcast extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// your code here
}
now create an object of this class instead on broadcastReceiver which you are
MyBroadcast mReceiver = new MyBroadcast();
prefer doing this in oncreate method of you activity.
now you can make a call to register receiver method as shown below:
registerReceiver(mReceiver,intentFilter);
Error 1
localString1 needs to be defined and initialized
Error 2
0x0 is a hexadecimal value, thus cant be a assigned to a variable of type BroadcastReceiver
You should replace this code:
registerReceiver(mReceiver, localString1);
with this:
static final String SOME_ACTION = "com.yourcompany.yourapp.SOME_ACTION";
IntentFilter filter = new IntentFilter(SOME_ACTION);
registerReceiver(mReceiver, filter);
As for the second error what are you trying to do?
if you want to set mReceiver to null just give:
mReceiver = null;

Categories

Resources