I have inported an Eclipse app into Studio. When I run the app I get the fatal error:
Attempt to invoke virtual method 'void android.nfc.NfcAdapter.enableForegroundDispatch(android.app.Activity, android.app.PendingIntent, android.content.IntentFilter[ ], java.lang.String[ ][ ])' on a null object reference
As far as I can tell the error is caused by the mAdapter.enableForegroundDispatch method below, because one ore more of it's parameters is null. I put a breakpoint there and run the debugger in the emulator. It never hits the breakpoint. I also set the debugger to break on any exception and it doesn't. the App starts and I get the "unfortunately the app has stopped" message.
@override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
//Used for DEBUG : Log.v("NFCappsActivity.java", "ON RESUME NFC APPS ACTIVITY");
mPendingIntent = PendingIntent.getActivity(this, 0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
}
Thanks for the help.
Rich
Related
Hi there,
I'm working on my LeoParts and integrated ROM Updater in my ROM.
Now I want my LeoParts to launch ROM Updater on a PreferenceScreen.
I tried two things:
1. With the XML
Code:
<PreferenceScreen
android:key="rom_update"
android:title="@string/updates_title"
android:summary="@string/updates_summary">
<intent android:action="android.intent.action.MAIN"
android:targetPackage="org.elegosproject.romupdater"
android:targetClass="org.elegosproject.romupdater.VersionsList"/>
</PreferenceScreen>
2. With the Java code
Code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("org.elegosproject.romupdater", "org.elegosproject.romupdater.VersionsList");
startActivity(intent);
3. From the shell
Code:
am start -a android.intent.action.MAIN -n org.elegosproject.romupdater/.VersionsList
This last one works, but I would like something less hacky...
Here is the error I get from a logcat:
Code:
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cmp=org.elegosproject.romupdater/.VersionsList } from ProcessRecord{407e1ed8 775:com.leonnib4.leo_parts/10044} (pid=775, uid=10044) requires null
I know this as something to do with permissions, most likely in the AndroidManifest.xml, but I can't get it to work.
Please help me out
Thx
I got arround by doing:
Code:
Intent intent = getPackageManager().getLaunchIntentForPackage("org.elegosproject.romupdater");
if (intent != null) {
toast.show();
String commands[] = {
"am start -a android.intent.action.MAIN -n org.elegosproject.romupdater/.VersionsList"
};
// Execute in a shell
} else {
// Toast notif "ROM Updater is missing"
}
But that is still hacky and I'm still looking for a better way to do this...
Thx
I try to activate WifiAP (wifi tethering) via my application code. Since there is no direct API for it, it needs to be done via reflection.
Using the approach as mentioned on StackOverflow.com.
It works fine on Nexus One, HTC Desire, Huawei Ideos (all running 2.2.x), but it doesn't run on the SGS with 2.2.1.
Checking the android.net.wifi.WifiManager class via reflection, I see that Samsung has obviously modified the class and it has 57 instead of 37 methods.
Unfortunately the modified WifiManager class is not open sourced by Samsung and the error messages don't give any details or stack trace.
I am looking for somebody who can enable WifiAP activation programmatically on the SGS, eventually by modifying the underlying system, since I'm myself am an application developer, not working on system level. Also considering contract work!
There are two things I am trying:
1) activate WifiAP directly via reflection:
Code:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
Log.d(TAG, "enableMobileAP methods " + wmMethods.length);
for(Method method: wmMethods){
Log.d(TAG, "enableMobileAP method.getName() " + method.getName());
if(method.getName().equals("setWifiApEnabled")) {
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = "MyWifiAP";
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
try {
Log.d(TAG, "enableMobileAP try: ");
method.invoke(wifi, netConfig, enabled);
if (netConfig.wepKeys!=null && netConfig.wepKeys.length>=1)
Log.d(TAG, "enableMobileAP key : " + netConfig.wepKeys[0]);
Log.d(TAG, "enableMobileAP enabled: ");
mIsWifiEnabled = enabled;
} catch (Exception e) {
Log.e(TAG, "enableMobileAP failed: ", e);
}
}
}
Attached are the logfiles I am getting on Nexus One (success) and SGS (failure).
The noticable part in the SGS log are maybe these lines:
Code:
D/WifiService( 2818): setWifiApEnabled: allowWifiAp: true
E/WifiService( 2818): setWifiApEnabledState 2
E/SoftapController( 2681): Softap already stopped
E/WifiService( 2818): Exception in stopAccessPoint()
E/WifiService( 2818): setWifiApEnabledState 4
D/WifiAp (23523): enableMobileAP key : null
2) another thing I try - since the modified SGS WifiManager (only that one) has a method named 'showApDialog' without any parameter taken, I try to call that method, but this also just gives an 'Error' in the log without further explanation.)
Code:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
Log.d(TAG, "enableMobileAP methods " + wmMethods.length);
for(Method method: wmMethods){
Log.d(TAG, "enableMobileAP method.getName() " + method.getName());
if(method.getName().equals("showApDialog")) {
WifiConfiguration netConfig = new WifiConfiguration();
try {
method.invoke(wifi);
} catch (Exception e) {
Log.e("Main", "Can't open ap dialog.", e);
}
}
}
I have been able to enable an existing configuration by passing null as the WifiConfiguration parameter to the setWifiApEnabled method.
I had the same problems as you when I tried to use this function to create a new configuration. I also failed when I tried to pass the existing configuration as a parameter, after invoking getWifiApConfiguration.
I noticed in the Android source code that there is a similar function called setWifiApConfiguration, but calling wifiManager.getClass().getMethod("setWifiApConfiguration") throws NoSuchMethodException when I run it on my SGS.
I also wondered if I can use addNetwork to create a new configuration, but suspect it is only for connecting to APs, rather than creating one.
As I only wanted to toggle the existing configuration, which I have now achieved, I stopped investigating at this point.
Hope that helps,
Good luck...
Yes, I've also got it to work meanwhile but didn't update this thread. You're right, the problem were the configuration, which had to be left empty. Cheers
thank you for sharing this, it really made my day
it seems that (on Android 2.3.4) it only works if you first disable the WiFi (Nexus One).
Hey, I'm fairly new to using android studios and I am trying to make an app that updates its display every few seconds.
The method I am currently using is to update when a button is pressed using the following:
public void Refreshing(View view) {
// do stuff here...
}
Where Refreshing is the onClick of a button I placed.
How can I use a similar format but instead of doing things on a button press just have it done automatically after a few seconds?
You can use a android.os.Handler for that. It allows you to interact with the event loop and post a Runnable that will be executed after a given delay
In your Activity's onCreate() method:
Code:
final int delayMs = 1000;
final Handler handler = new Handler(this);
final Runnable runnable = new Runnable() {
@Override public void run() {
// Call your refresh method
Refreshing();
// Restart the timer
handler.postDelayed(runnable, delayMs);
};
}
handler.postDelayed(runnable, delayMs);
Thanks, this is exactly what I'm looking for. I add the code to the onCreate but I had an semicolon error. So I added another semicolon but its having trouble with a few other parts. Here is what I have.
Code:
final int delayMs = 1000;
final Handler handler = new Handler([B]this[/B]);
final Runnable runnable = new Runnable() {
@Override public void run() {
// Call your refresh method
Refreshing();
// Restart the timer
handler.postDelayed([B]runnable[/B], delayMs);
};
};
handler.postDelayed(runnable, delayMs);
The error listed is:
Error : (134, 33) error: no suitable constructor found for Handler(MainActivity)
constructor Handler.Handler(Looper,Callback) is not applicable
(actual and formal argument lists differ in length)
constructor Handler.Handler(Looper) is not applicable
(actual argument MainActivity cannot be converted to Looper by method invocation conversion)
constructor Handler.Handler(Callback) is not applicable
(actual argument MainActivity cannot be converted to Callback by method invocation conversion)
constructor Handler.Handler() is not applicable
(actual and formal argument lists differ in length)
The words I bolded are underlined in red in Android Studios.
this ----------------has a constructor error
runnable ----------has a initialization error
I tried troubleshooting for a little bit and took out the this and the only error left is with runnable. It says: Error: (141, 37) error: variable runnable might not have been initialized
Yep, I wrote the code without testing it. It might need some adaptation.
Here is a version that should work:
Code:
final int delayMs = 1000;
final Handler handler = new Handler([B]this[/B]);
final Runnable runnable = new Runnable() {
@Override public void run() {
// Call your refresh method
Refreshing();
// Restart the timer
handler.postDelayed([B]this[/B], delayMs);
};
};
handler.postDelayed([B]runnable[/B], delayMs);
Thank you so much, it worked. I just had to change
Code:
final Handler handler = new Handler(this);
to
Code:
final Handler handler = new Handler();
First of all, we need to reboot the device by button click.
Device: Mediatek MT6737M Android 6.0 running Marshmallow
I currently have three devices. One rooted with Magisk another SuperSU the third with Kingroot. In the settings on all three root managers I have it set to grant all permissions. When we try and restart Kingroot flashes a prompt to allow permission which we can work around but Kingroot is riddled with garbage that we don't want. We want to use Magisk or SuperSU because they are clean with less baggage. Magisk and SuperSU however throw an error: java.io.IOException:
Code:
Error running exec(). Command: [/system/bin/su, -c, reboot now] Working Directory: null Environment: null.
This is the code we're using to access PowerManager:
Code:
void reboot() {
if (reboot) {
try {
Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "reboot"});
proc.waitFor();
} catch (Exception ex) {
Log.e(TAG, "Error ", ex);
}
}
}
Is there a way to grant permission to this process using either Magisk or SuperSU? If not is there another rooting management tool or software that is clean and malware free that can achieve that?
NOTE: Code by senior developer I am a Baby, Jr dev here tasked with finding the answer. Please be patient if I ask dumb questions.
Ok so first thing I'll start by saying that you should be making su calls in a background thread. The su call is a blocking call so it will block the UI thread which can cause ANRs (crashes) and overall bad user experience. AsyncTask is perfect for su calls to run in the background instead.
You should also stick with Magisk as it is actively being developed and is the current standard.
Let's get started. Create a new class and call it RootShell.java. Then copy the following code into it.
Java:
class RootShell {
private static void exec(String[] cmd) {
new Execute().execute(cmd);
}
private static class Execute extends AsyncTask<String, Void, Void> {
Override
protected Void doInBackground(String... cmds) {
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd + "\n");
}
os.writeBytes("exit\n");
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
Keep in mind that XDA has awful code formatting so the Override should have the @ symbol in front of it.
Now you can pass single or multiple root commands to this class which will be executed in a background thread. Here's how...
Lets say you're using a button in another class to reboot the phone. This is what it would look like...
Java:
private void rebootButton(View view) {
String[] cmd = { "reboot" };
RootShell.exec(cmd);
}
To pass multiple commands your button would look like this.
This is to reboot into safe mode...
Java:
private void rebootSafeMode(View view) {
String[] cmd = {
"setprop persist.sys.safemode 1",
"setprop ctl.restart zygote"
};
RootShell.exec(cmd);
}
For more info on how to properly su you should check out the following link...
https://su.chainfire.eu/
Hope this helps you with your app and good luck.
Square Enix's Final Fantasy 3 is known to crash when any xposed modules are enabled.
I would like to work around the problem by preventing the failing method to be called at all.
When starting FF3, it crashes with:
Code:
E/AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: main
java.lang.SecurityException: 1317 does not have permission:android.permission.CLEAR_APP_USER_DATA to clear datafor process:org.example.xposed.module
at android.os.Parcel.readException(Parcel.java:1425)
at android.os.Parcel.readException(Parcel.java:1379)
at android.app.ActivityManagerProxy.clearApplicationUserData(ActivityManagerNative.java:2889)
at com.android.commands.pm.Pm.runClear(Pm.java:1126)
at com.android.commands.pm.Pm.run(Pm.java:116)
at com.android.commands.pm.Pm.main(Pm.java:75)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:135)
at dalvik.system.NativeStart.main(Native Method)
Now I would like to make "android.app.ActivityManagerProxy.clearApplicationUserData" simply return true if the package name is one of the xposed modules that are running here.
I tried that with:
Code:
@Override
public void initZygote(StartupParam startupParam) throws Throwable
{
XposedHelpers.findAndHookMethod(
"android.app.ActivityManagerProxy", null, "clearApplicationUserData",
String.class, "android.content.pm.IPackageDataObserver", int.class,
new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
XposedBridge.log("clearApplicationUserData");
param.setResult(true);
}
}
);
}
Unfortunately, the method is never called (I do not see the log message), and the crash still occurs.
I tried it both in initZygote and handleLoadPackage.
I'm running on Android 4.1.2 with xposed v50 (bridge v47).
Am I missing something? Does "android.app.ActivityManagerProxy" need some special handling/registration?
Hooking into command line tools needs IXposedHookCmdInit which is not supported anymore:
https://forum.xda-developers.com/xposed/hook-cmds-pm-t2955122
https://forum.xda-developers.com/xposed/xposed-api-changelog-developer-news-t2714067#4
@rovo89 wrote
Only two modules used it, both got rid of it without any loss of functionality.
Click to expand...
Click to collapse
This here is an exception: The command init hook is needed, because ActivityManagerProxy.clearApplicationUserData() already does security checks before it hands the command off to PackageManagerService.clearApplicationUserData.
So I had to go the hard way and find an old XposedBridgeApi-52.jar that let me compile an xposed module with IXposedHookCmdInit and initCmdApp inside it, just to hook into the clearApplicationUserData used by the "pm" cli tool.
It actually worked and I can now run "pm clear mypackage" from every user and always get a "success" message.
Unfortunately, Final Fantasy 3 still exits:
I/Xposed: clearApplicationUserData: de.cweiske.ouya.plainpurchases
D/AndroidRuntime: Shutting down VM
I/AndroidRuntime: NOTE: attach of thread 'Binder_3' failed
D/dalvikvm: JIT code cache reset in 0 ms (86452 bytes 1/0)
D/dalvikvm: Debugger has detached; object registry had 1 entries
D/Zygote: Process 1572 exited cleanly (1)
I/ActivityManager: Process com.square_enix.android_OUYA.FFIII (pid 1572) has died.
W/ActivityManager: Force removing ActivityRecord{420f33f0 com.square_enix.android_OUYA.FFIII/com.square_enix.FFIII_J.MainActivity}: app died, no saved state
I am beginning to think that the lib is Square Enix' xposed protection, and that the forced crash was only a red herring so that I could waste time
Blog post: http://cweiske.de/tagebuch/square-enix-securityexception.htm