jNetPcap on Android: problem with findAllDevs method! - android

I've sucessfully compiled jNetPcap as a shared library for Android. I have made a simple application using this code: http://jnetpcap.com/examples/classic to test the API.
The problem is that when I call the method findAllDevs and exception is raised with this message: "Can't read list of devices, error issocket: Permission denied"
I cannot understand the reason, since I have made a call in the first part of my program so as to get root permissions for my application and I test my application to a rooted phone. When I run the application, a pop up is raised with this message:"SnifferApp has been granted Superuser permissions" and then the exception occurs.
Any ideas?
Here is a piece of my code:
Process p = Runtime.getRuntime().exec("su");
/*try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // do nothing for 1000 miliseconds (1 second)
*/
try {
System.loadLibrary(JNETPCAP_LIBRARY_NAME);
}
catch (UnsatisfiedLinkError e) {
System.out.println("Native code library failed to load.\n" + e);
}
/***************************************************************************
* First get a list of devices on this system
**************************************************************************/
int r = Pcap.findAllDevs(alldevs, errbuf);
r = Pcap.findAllDevs(alldevs, errbuf);
if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
tv.append("Can't read list of devices, error is" + errbuf
.toString());
setContentView(tv);
return;
}

As far as I understand, you're creating a new process and getting superuser permission, but your app doesn't have them.
Try adding this permission in your manifest file:
<uses-permission android:name="android.permission.INTERNET" />
as I guess findAllDevs is going to open the network devices on the phone.

Related

How do I see Android logs that were logged before connecting to Android studio

Is there a way to see Android logs that were logged before connecting to the Android Studio?
I have an app that tracks GPS location. The issue is that it terminates after some time and I get the Android system message that says "Application Terminated"
I want to see what went wrong and where. When I connect Android studio later, it shows logs that happened from time it is connected.
I want the logs from past.
There are couple of things you can try to resolve:
1) I generally write logs when I have such cases to test.
public static void writeToFile(String msg) {
Log.d("MyApp", msg);
try {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
FileOutputStream dio = null;
File file = new File(Environment.getExternalStorageDirectory(), "MyApp_File_Logs.txt");
if (file != null) {
try {
dio = new FileOutputStream(file, true);
dio.write((msg+"\n").getBytes());
dio.close();
dio = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
Log.e("MyApp", "Exception in writeToFile(): " + e.getMessage());
}
}
So instead of Log.d , Use this Utility method. You can add time in the message to be more precise.
Done forget to give write external storage permission to you app
2) Once you see crash , go to command prompt
and use the below commands:
adb shell
logcat -b crash
You might see something in this, if you connect within a minute or so
3) Increase the "Logger Buffer Size " in your developer options , but in this case you will see delay in getting logs when you connect your device to adb and open logcat.
you can add a log module into your app,let log module output your app's log into local file,like logger

Deleting call log of a be number

I am using the following code to delete a particular number from the call log, but it is not working on Android Marshmallow:
String queryString = "NUMBER=" + number;
try {
context.getContentResolver().delete(CallLog.Calls.CONTENT_URI, queryString, null);
} catch (Exception e) {
e.printStackTrace();
}
How can I delete the call log on Android Marshmallow?
On Marshmallow you will need to request the permissions from user to read and write from call log
See official doc link below on how to request permissions
http://developer.android.com/training/permissions/requesting.html

How can i delete and rename a file in Android?

Hello I have been looking for ways to delete or rename an specific file in the internal storage of the cellphone. Specifically my targets are the files in the waze folder, that are in the root folder of the internal storage. As I said, I look for more information about this but nothing works for me, so I think that my error might be in the path i'm using. Here is my code:
TO RENAME:
file_Path = "/data/data/waze"
File from = new File(file_Path, "currentFileName");
File to = new File(file_Path, "newFilename");
from.renameTo(to); //this method returns me False
TO DELETE:
file_Path ="/data/data/waze/file"
File file = new File(file_Path);
boolean deleted = file.delete();
I try a lot of ways to do this, but this is the one I think is near to get it. So If anyone of you could point me my mistake/s I would thank you! A hug from Costa Rica!
You do not have read or write access to files on internal storage other than your own app's files. You cannot rename or delete files from another app, such as Waze.
The exception is that on rooted devices, you can ask to fork processes with superuser privileges, and those processes would have device-wide read/write access.
For completing #CommonsWare answer, you can check if the device is rooted, then do the methods or something else.
Here is an example,
Taken from : http://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/
Process p;
try {
// Preform su to get root privledges
p = Runtime.getRuntime().exec("su");
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() != 255) {
// TODO Code to run on success
toastMessage("root");
}
else {
// TODO Code to run on unsuccessful
toastMessage("not root");
}
} catch (InterruptedException e) {
// TODO Code to run in interrupted exception
toastMessage("not root");
}
} catch (IOException e) {
// TODO Code to run in input/output exception
toastMessage("not root");
}
Or you can take a look at :
http://su.chainfire.eu/#how
https://github.com/Chainfire/libsuperuser
and also, use the following permission in your manifest too:
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
Or a good example is available on Github:
https://github.com/mtsahakis/RootChecker

Sony: SampleNotificationExtension: Sample app throwing exceptions

I'm attempting to send notifications to Sony LiveWare devices
I've installed: com.sonyericsson.extras.liveware.extension.notificationsample, but I get errors and exceptions while trying to run it:
Load the preference activity onto the device
Check "Active" on the preferences screen
The following method is called in SampleExtensionService:
private void addData() {
Random rand = new Random();
int index = rand.nextInt(5);
String name = NAMES[index];
String message = MESSAGE[index];
long time = System.currentTimeMillis();
long sourceId = NotificationUtil
.getSourceId(this, EXTENSION_SPECIFIC_ID);
if (sourceId == NotificationUtil.INVALID_ID) {
Log.e(LOG_TAG, "Failed to insert data");
return;
}
String profileImage = ExtensionUtils.getUriString(this,
R.drawable.widget_default_userpic_bg);
ContentValues eventValues = new ContentValues();
eventValues.put(Notification.EventColumns.EVENT_READ_STATUS, false);
eventValues.put(Notification.EventColumns.DISPLAY_NAME, name);
eventValues.put(Notification.EventColumns.MESSAGE, message);
eventValues.put(Notification.EventColumns.PERSONAL, 1);
eventValues.put(Notification.EventColumns.PROFILE_IMAGE_URI, profileImage);
eventValues.put(Notification.EventColumns.PUBLISHED_TIME, time);
eventValues.put(Notification.EventColumns.SOURCE_ID, sourceId);
try {
getContentResolver().insert(Notification.Event.URI, eventValues);
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, "Failed to insert event", e);
} catch (SecurityException e) {
Log.e(LOG_TAG, "Failed to insert event, is Live Ware Manager installed?", e);
} catch (SQLException e) {
Log.e(LOG_TAG, "Failed to insert event", e);
}
}
The first problem is that "long sourceId = NotificationUtil.getSourceId(this, EXTENSION_SPECIFIC_ID)" returns -1
I'm not sure how to change, but the docs say it is an optional field. So I removed it to see if I could get any further:
When removing the sourceId code I get an exception on the insert call:
"java.lang.SecurityException: You do not have sufficient permission to perform a insert"
My Environment:
HTC One V paired with a LiveView wrist pager
http://www.amazon.co.uk/Sony-Ericsson-0001516170-LIVE-VIEW/dp/B00477X6DA
I've got the followings apps installed
LiveView
SmartConnect(LiveWare Manager)
I'd appreciate any help getting this sample to work or if anyone knows of a more straight forward approach to doing simple text notifications for LiveWare.
EDIT:
Made some progress. Found an emulator here:
android-sdk\add-ons\addon-sony_add-on_sdk_1_0-sony-16\apk_files
I installed that on the phone. The "SampleExtensionService" works fine in the emulator.
My assumption now is that add-on sample is not compatible with the LiveView device.
The LiveView is unfortunately not supported by the SmartExtension APIs. Instead, there is a specific developer kit for the LiveView here.
If you would, in the future, buy a newer wearable device from Sony Mobile, a notification extension based on the Smart Extension APIs would be usable for several devices including SmartWatch, SmartWatch 2, SBH50, SBH52 etc.

Blank File is saved when using screencap programatically in Android

code snippet
void takeSnapShot()
{
Process process = null;
try
{
process = Runtime.getRuntime().exec("/system/bin/screencap -p /sdcard/snapshot/test_2.png" );
try
{
process.waitFor();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am gettin an empty file saved whereas If i use the same command through adb shell, i get my screen captured.
Any help will be appreciatable
Edit: my previous answer was mistaken, any app can use the screen capture command.
It might be a permission issue. Are you sure you have permission to write to sdcard?
Check this post, which covers your topic:
How to run android system app without root permisson?
I solved this by adding the following permission to my Manifest.
<uses-permission
android:name="android.permission.READ_FRAME_BUFFER"
tools:ignore="ProtectedPermissions"/>
NOTE: This is a protected permission, my application is a system app and also signed using the platform key.

Categories

Resources