Help me translate this coding - Java for Android App Development

I'm learning how to manage data that I pull from DB (MYSQL) from this coding. I tried to figure out from free source coding but got stuck on this function, can anybody explain to me flow of this coding?
Code:
protected void onPostExecute(Void aVoid) {
name = names.split(":");
email = emails.split(":");
phone = phones.split(":");
combinedArray = combinedText.split(":");
listView.setAdapter(new ArrayAdapter<String>(RetrieveData.this,
android.R.layout.simple_list_item_1, combinedArray));
progressDialog.dismiss();
}
and when I tried to use this code, red line prompt out and saying that cannot resolved this constructor on if i change
Code:
listItems
to
Code:
names
variables on this
Code:
adapter=new ArrayAdapter<String>(this,
R.layout.list_item, R.id.txtitem, listItems);
I don't understand why I need to use 'split' to pull out the output on listview.

Related

Native Callbacks (C# and C++)

Hi all,
From C# I could call unmanaged C++ export functions within a self written DLL. Now I try to operate with callback; so C++ code will be able to call selected C# functions.
Unfortunately theres occure an error at runtime:
NotSupportedException
Application::Run+0xf
MainForm::Main+0xa
Here is my code:
C#
Code:
public delegate bool MyCallback(int res);
[DllImport("mydll.dll", CharSet = CharSet.Unicode)]
public extern static int Export_Double(int input, MyCallback cb);
private void menuItem19_Click(object sender, EventArgs e)
{
MyCallback cb = new MyCallback(MyRealCallBack);
int res = Export_Double(3,cb);
}
public static bool MyRealCallBack(int res)
{
return true;
}
C++ DLL
Code:
extern "C" __declspec(dllexport) int __stdcall
Export_Double(int input, bool (*myCB) (int))
{
int result = input * 2;
//over callback
myCB(result);
//over return
return result;
}
Do somebody know whats wrong?
Thanks a lot!
Br adi
PS: I had read http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/
if you get not usefull response then i sugest you try
http://www.codeguru.com/forum/index.php?
it's more purely code related then this site
Hi,
yeahh.. thats a good forum! Thanks for your help!
I saw that ".NET Compact Framework" don't support callbacks of type "delegate".
There exists an other way by using "MessageWindow Class".
Here is a usefull paper on MSDN:
Asynchronous callbacks from native Win32 code
BR Adrian[/url]
.NET Compact Framework 2.0 supports marshalling of delegates as callbacks. See the docs on Marshal.GetFunctionPointerForDelegate for more information.
Thanks for your tip. I installed CF 2.0 and adapt my code:
Code:
private void menuItem19_Click(object sender, EventArgs e)
{
MyCallBakcFunc b1 = new MyCallBakcFunc(MainForm.MyRealCallBack);
//NotSupported Exception on that line
IntPtr funcptr = Marshal.GetFunctionPointerForDelegate(b1);
.....
Unfortunately I get the same error (on both emulator and device):
NotSupportedException
0x80131515
Its strange. I have read in documentation, that this will be supported from .Net 2.0 and .Net CF 2.0!
Did you use it by your own?
Thanks!
BR adrian

Need help with wget implantatet in App

Hi,
I have a wget command that I want to implantate in an app. the command looks like this
It's executed in a terminal:
Code:
wget --post-data='loginwe=YOURLOGIN&passwordwe=YOURPASS' \
> --save-cookies=my-cookies.txt --keep-session-cookies \
> https://HOSTNAME?cmd=ACTION
the page information have to be saved in a string that will be searched for information.
Can someone help me with the code that can do this job?
UPDATE!
This is what I have tried to incorporate in my app. but I do not get logged in.
Code:
public String getSaldo(String number, String passy) {
HttpClient client = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("https://HOSTNAME?cmd=ACTION");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("phoneNumber", number));
params.add(new BasicNameValuePair("password", passy));
try {
postRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = client.execute(postRequest, responseHandler);
Is there someone who can see why the httpClient command isn't working?
Why not instead try to leverage org.apache.http.client classes and avoid the messiness of using a shell?
http://developer.android.com/reference/org/apache/http/package-summary.html
If you want to go the shell route, check out:
java.lang.Runtime.getRuntime().exec();
it's fairly easy to see what you're getting at but I just thought I'd mention that implantatet is most definitely not a word. not in english anyway..
Thanks Info Updatet

[Q] Writing to your Apps data folder

im trying to write a Serializable Object to the data folder of my app. I was using this code to specify the path to the file and write the Serializable Object
Code:
/**
* writeMissedCalls()
*
* @param context - the Context of the application
* @return if the missedCalls were written to file successfully
*/
public static boolean writeMissedCalls(Context context) {
String filename = context.getApplicationInfo().dataDir + "/" + MCWUtils.MCW_DATA_FILE;
FileOutputStream fos;
ObjectOutputStream out;
try {
fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
out = new ObjectOutputStream(fos);
out.writeObject(MissedCallWidget.missedCalls);
out.close(); }
catch (FileNotFoundException e) { return false; }
catch (IOException e) { return false; }
return true;
}
when i try to write this Object in the onDisabled() of my AppWidgetProvider class i get an error of this sort
java.lang.RuntimeException: Unable to start receiver com.tsb.fistfulofneurons.missedcallwidget.MissedCallWidget: java.lang.IllegalArgumentException: File /data/data/com.tsb.fistfulofneurons.missedcallwidget/missed_calls.dat contains a path separator
do i not need to specify the path to my apps data folder? will the openFileOutput() specify the path to the data for me?
so instead of passing the path "/data/data/com.tsb.fistfulofneurons.missedcallwidget/missed_calls.dat" just pass "missed_calls.dat"?
thanks!
I've not tried to open a file in this manner, but I would guess that it defaults to the apps data directory. Why not give it a try and see?
Gene Poole said:
I've not tried to open a file in this manner, but I would guess that it defaults to the apps data directory. Why not give it a try and see?
Click to expand...
Click to collapse
yea it appears to default to the /data/data folder for your package. the documentation appears to be lacking. thanks

Receiving error: Cannot convert from element type Object

Hello,
As the title states, I'm receiving an error that says "Cannot convert from element type Object to Bluetooth Device. The following is the highlighted code:
Code:
if (pairedDevices.size() > 0) {
findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);//make title viewable
for (BluetoothDevice device : pairedDevices) {
mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
} else {
mPairedDevicesArrayAdapter.add("no devices paired");
}
I have a feeling it has something to do with java generics, but I'm not quite sure how to fix it. Would anyone be able to offer help?
Thanks
theBasher91 said:
Code:
for (BluetoothDevice device : pairedDevices)
Click to expand...
Click to collapse
Well I would suspect that pairedDevices is a list or array of type Object? Not sure as you dont post the actual error or line numbers... but cast within the for loop if this is the case
Code:
for (Object item : pairedDevices)
{
BluetoothDevice device = (BluetoothDevice) item;
}
Just a thought
Or, assuming that pairedDevices is an ArrayList or other type that implements the collections interface, you're best bet would be to ensure that it is parameterized correctly.
For example:
Code:
ArrayList<BluetoothDevice> = new ArrayList()<BluetoothDevice>

[Library] Cmd library: Execute Your Java Codes With Root Access Privileges

Synopsis:
Have you ever wanted as root apps developer to use your favourite Java codes instead of bash/shell commands?
Have you grown tired of Runtime.getRuntime.exec("su") and ProcessBuilder("su").start()?
I did and still do, that's why I've been searching for a solution for my problem until I found out in AOSP source code that some of *.java files run with root access, tracking the way they execute them with root access led me to make this simple library.
Description:
Cmd library -short for Command- is an open source (licensed under Apache licence V2) that allows you to execute java commands with root access privileges by passing package name, full class name and whether your app is a system app or not in just two lines of codes.
For instance, suppose you want to flash a *.img recovery image, the first thing comes to your mind is this code:
Code:
busybox dd if=/path/to/recovery.img of=/path/to/recoveryPartition
lets say /sdcard/recovery.img is the file path and /dev/block/mmcblk0p12 is the partition (Samsung GALAXY Ace Plus GT-S7500)
Here is how to implement it:
Code:
...
Proccess proc = Runtime.getRuntime().exec("su");
OutputStream stream = proc.getOutputStream();
stream.write("busybox dd if=/sdcard/recovery.img of=/dev/block/mmcblk0p12".getBytes());
stream.flush();
...
Pretty simple isn't it, now suppose you don't know anything about bash codes, what will you do?
Let me answer this question:
1) Learn some bash.
2) Use Cmd library with java codes.
Here is the same implementaion with Cmd library:
Code:
...
JavaRoot root = JavaRoot.newInstance(getPackageName(), RootCommands.class.getName(), false);
root.execute("/sdcard/recovery.img", "/dev/block/mmcblk0p12")
...
And in RootCommands.java file (should not be an inner class):
Code:
package com.bassel.example;
import com.bassel.cmd.Command;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Main extends Command
{
private byte[] buffer;
private File recovery, partition;
private FileInputStream in;
private FileOutputStream out;
//static main method implementation is a must
@Override
public static void main(String[] args)
{
//required to start
new Main().run(args);
}
//the begging just like onCreate
@Override
public void onRun()
{
if(argsCount() < 2) showUsage();
System.out.println(String.format("Flashing %s to %s", getArg(0), getArg(1)));
recovery = new File(nextArg());
partition = new File(nextArg());
buffer = new byte[1024 * 1024];
try
{
in = new FileInputStream(recovery);
out = new FileOutputStream(partition);
while(in.read(buffer) > 0)
{
out.write(buffer);
out.flush();
}
in.close();
out.close();
System.out.println("Flashed successfully!");
}
catch (Exception e)
{e.printStackTrace(System.err);}
}
//called upon calling showUsage()
@Override
public void onShowUsage()
{
System.err.println("Two args are needed!");
}
//called once an exception is caught
@Override
public void onExceptionCaught(Exception exception)
{
exception.printStackTrace(System.err);
}
}
Quite lengthy but sometimes we prefer using Java codes to bash ones, don't we?
Here is another example for listing /data files:
Code:
package com.bassel. example;
import java.io.File;
//Without extending Command.class
public class Main
{
@Override
public static void main(String[] args)
{
String[] files = new File("/data").list();
for(String file : files)
{
System.out.println(file);
}
}
}
Usage in depth:
Check root access:
Cmd.root();
No root:
Cmd.SH.ex(String command, String... args);
Cmd.SH.ex(String[] commands);
Cmd.SH.ex(List commands);
With root access:
Cmd.SU.ex(String command, String... args);
Cmd.SU.ex(String[] commands);
Cmd.SU.ex(List commands);
JavaRoot java = JavaRoot.newInstance(String packageName, String className, boolean isSystemApp);
java.execute(String... args);
java.executeInBackground(String... args);
All previous methods aside of the last one:
java.executeInBackground(String... args);
return an instance of type Output that has the following methods:
Code:
boolean success() //returns true if process exit value = 0 else false
String getString() //returns output in String format
String[] getArray() //returns output in String Array format
List getList() //returns output in String List format
int getExitValue() //returns process exit value
String toString() //returns process status and output in String format
Converting:
String string = ...;
String[] array = ...;
List list = ...;
String s1 = Convert.array2string(array);
String s2 = Convert.list2string(list);
String[] a1 = Convert.list2array(list);
String[] a2 = Convert.string2array(string);
List l1 = Convert.array2list(array);
List l2 = Convert.string2list(string);
Library GitHub link:
Cmd (licensed under Apache License, Version 2.0)
Examples:
Window Manipulator

Categories

Resources