Socket Programming on HTC Magic - myTouch 3G, Magic Android Development

Hi, I have made an application on the phone which collects the data from the built-in sensors, and now I'm trying to use socket programming to send the data to my laptop. Basically, I put the following socketing code in my application code and wrote another server program on my laptop to set up the connection between the cellphone and the laptop. But it doesnt seem to work....my phone shows a debug message "No I/O"...meaning, I guess, the phone is actually not sucessfully connected to the laptop. Could anybody pls teach me the right way to do this? communication either through wifi (my laptop and phone both already logged onto the campus network), or usb(the phone is directly connected to the laptop by a usb cable). Thank you!
Client code on the phone:
public void listenSocket(){
//Create socket connection
try{
socket = new Socket("localhost", 4444);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
} catch (UnknownHostException e) {
socketMsg.setText("Unknown host: localhost");
} catch (IOException e) {
socketMsg.setText("No I/O");
}
}
Server code on the laptop:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
class SocketServer1 {
public static void main(String[] args){
ServerSocket server = null;
Socket client = null;
BufferedReader in = null;
PrintWriter out = null;
try{
server = new ServerSocket(4444);
} catch (IOException e) {
System.out.println("Could not listen on port 4444");
System.exit(-1);
}
System.out.println("Server is ready");
try{
client = server.accept();
System.out.println("Server is not accepting data");
} catch (IOException e) {
System.out.println("Accept failed: 4444");
System.exit(-1);
}
try{
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
} catch (IOException e) {
System.out.println("Accept failed: 4444");
System.exit(-1);
}
//Clean up
try{
in.close();
out.close();
server.close();
} catch (IOException e) {
System.out.println("Could not close.");
System.exit(-1);
}
}
}
PS. I guess the problem may be the server_name and port_number set on my phone are not correct....

After a quick look, and please take into consideration I never did Android programming, in the client, instead of trying to connect to the server you are trying to connect to localhost, wich means, you are trying to connect to your phone. You need to replace localhost with the ip of your server. The port seems to be ok since you are opening a server socket in your server in the port 4444

helderp said:
After a quick look, and please take into consideration I never did Android programming, in the client, instead of trying to connect to the server you are trying to connect to localhost, wich means, you are trying to connect to your phone. You need to replace localhost with the ip of your server. The port seems to be ok since you are opening a server socket in your server in the port 4444
Click to expand...
Click to collapse
Hi thank you helderp! I think I've figuired it out. what you suggested is one of the reasons.
just for your info, the other BIG reason, which is actually about android programming, is that in "AndroidManifest.xml" file should add in "<uses-permission android:name="android.permission.INTERNET">"....

Related

C#: Make program run even though it's minimized

I'm programming an application (WM 6.1) that should do things regardless if the program is active or minimized (user pressed the X button). This is where my problem occurs, my program is set on "pause" when the program is minimized. I need it to run even though it's in the background. How to solve this?
threads
Hi Pytagoras,
I haven´t made a lot of C# programs, but I think that only the main thread is pause when the program is minimized, but the additional threads don´t. So if you need something running on background you need to implement it on an additional thread...
See you
Pytagoras said:
I'm programming an application (WM 6.1) that should do things regardless if the program is active or minimized (user pressed the X button). This is where my problem occurs, my program is set on "pause" when the program is minimized. I need it to run even though it's in the background. How to solve this?
Click to expand...
Click to collapse
Thanks for your reply
I'm new to threads, haven't really needed them before. So I found some examples:
Code:
class ThreadTest {
private void frmProgram_Load(object sender, EventArgs e)
{
Thread t = new Thread (WriteY);
t.Start(); // Run WriteY on the new thread
}
static void WriteY() {
while (true) Console.Write ("y"); // Write 'y' forever
}
}
Of course I loaded my own method instead of the WriteY example method described here. The thread get started in the Load event of the main form, so it should execute. However, it doesn't work when the program is minimized, and with this thread way of doing it, it doesn't work when it's opened either. (the program should notice a sms ticking in, and do something based on it).
Any help would be greatly appreciated (there should be a dedicated forum on xda for this kind of things / different programming languages..just a comment)
Try something like this... should work...
Code:
class ThreadTest {
private Thread t;
private void frmProgram_Load(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(WriteY));
t.IsBackGround = true;
t.Start();
}
static void WriteY() {
while (true) Console.Write ("y"); // Write 'y' forever
}
}
Strange, it still don't work. I even tried with the code you posted without changing any of it, and still it don't run. It won't even write to the console
The issue is that the console.write is not showing on "Output Window"... see the code below... that it keeping update the file even if minimized...
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace ThreadTest
{
public partial class Form1 : Form
{
private Thread t;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(WriteY));
t.IsBackground = true;
t.Start();
}
private void WriteY()
{
while (true)
{
FileStream fs = new FileStream("\\test.txt", FileMode.Create);
StreamWriter w = new StreamWriter(fs, Encoding.UTF8);
w.WriteLine(DateTime.Now.ToString());
w.Flush();
w.Close();
fs.Close();
}
}
}
}
Thank you for your help, but I get this when I run your code:
System.IO.IOException:
"The process can not access the file '\\test.txt' because it is being used by another process."
The possibility for that is that you are running the example more than once...
Pytagoras said:
Thank you for your help, but I get this when I run your code:
System.IO.IOException:
"The process can not access the file '\\test.txt' because it is being used by another process."
Click to expand...
Click to collapse
Ok, I don't know that went wrong, but I've created a new project, and here it worked.
However, it doesn't work with the sms detection I try to implement.
I'll post the code hoping anyone have some hints:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
namespace BackgroundTestApp
{
public partial class Form1 : Form
{
private Thread t;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Start a new thread hoping that this will detect sms even if it's minimized
t = new Thread(new ThreadStart(startMessageDetection));
t.IsBackground = true;
t.Start();
// This works, but only when the program isn't minimized
//startMessageDetection();
}
/// <summary>
/// Load the message detection (sms)
/// </summary>
private void startMessageDetection()
{
MessageInterceptor messageInterceptor = new MessageInterceptor();
messageInterceptor.InterceptionAction = InterceptionAction.NotifyAndDelete;
messageInterceptor.MessageReceived += new MessageInterceptorEventHandler(messageRecieved);
}
void messageRecieved(object sender, MessageInterceptorEventArgs e)
{
if (e.Message is SmsMessage)
{
MessageBox.Show("Message recieved!");
}
}
}
}
Hi Pytagoras,
Sorry, with this part I can´t help you... never coded using this class...
See you,
Pytagoras said:
Ok, I don't know that went wrong, but I've created a new project, and here it worked.
However, it doesn't work with the sms detection I try to implement.
I'll post the code hoping anyone have some hints:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
namespace BackgroundTestApp
{
public partial class Form1 : Form
{
private Thread t;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Start a new thread hoping that this will detect sms even if it's minimized
t = new Thread(new ThreadStart(startMessageDetection));
t.IsBackground = true;
t.Start();
// This works, but only when the program isn't minimized
//startMessageDetection();
}
/// <summary>
/// Load the message detection (sms)
/// </summary>
private void startMessageDetection()
{
MessageInterceptor messageInterceptor = new MessageInterceptor();
messageInterceptor.InterceptionAction = InterceptionAction.NotifyAndDelete;
messageInterceptor.MessageReceived += new MessageInterceptorEventHandler(messageRecieved);
}
void messageRecieved(object sender, MessageInterceptorEventArgs e)
{
if (e.Message is SmsMessage)
{
MessageBox.Show("Message recieved!");
}
}
}
}
Click to expand...
Click to collapse
I don't know what's happening, but I don't like the fact that messageInterceptor is local to startMessageDetection(). The thread will not continue forever, since it is not looping... the three operations in startMessageDetection() will be performed and the thread will cease. I would make messageInterceptor a member of the form, assign it in startMessageDetection(), and try it without the thread. Does this work?
try making it a process instead of an application. Processes are ment to be running in the background.

[APP] Zune software remote control

Instructions + download: http://forum.xda-developers.com/showpost.php?p=17254652&postcount=7
I have finally found a way to control the Zune software running on Windows. The Zune API is horrible so there are few(if any) programs that interface with the software externally. Today I came across the SendMessage method. The idea is your Android device is a big remote control for the Zune software. If you already have a media remote then this application isn't needed. I only have a remote on my laptop, not desktop so that's why I'm bothering to write it. I thought I would share it on XDA for free.
http://pastebin.com/C85isGsW - that was my test program. When I opened it my music paused(yay!).
Anyways this will be a 2-part system. The Windows app will run in the background(either as a service or in the system tray) and listen on some random TCP port for a connection. It will be relatively small, using less than 50MB RAM. This one uses 27MB right now(yes, C# is bloated).
The Android app will simply connect over the wifis or even over the internet(just remember to forward ports) and after a quick handshake it will be able to send and receive data from the service/app in tray. First I'll start with simple play/pause buttons and a volume slider and eventually I'll add all the interfaces listed here: http://msdn.microsoft.com/en-us/library/ms646275(v=vs.85).aspx
Step 1: install service or open the Windows program
Step 2: type computer IP in android app
Step 3: press play/pause or control volume etc. It will save the IP so you don't have to keep typing it in. In fact I will have a dropdown list so you can select different computers(HTPC, basement computer etc.)
I just started writing the program so it will by done by the end of the weekend. Figured I would create the thread since I know it will work.
inb4 zune sucks
Interested in seeing this.
Sent from my Transformer TF101 using XDA Premium App
yes dude yes!!! imso amped for this! thanks so much.
OK I got the windows side app 95% done... started the Android version and well.. I'm a noob. Looks like honeycomb makes you interface with TCP in a separate thread...
Windows server code:
Code:
hile (stop != 1)
{
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
//Console.WriteLine("Received: {0}", data);
if (Convert.ToString(data).CompareTo("PP") == 0) SendMessageW(hwnd, WM_APPCOMMAND, hwnd, (IntPtr)APPCOMMAND_MEDIA_PLAY_PAUSE);
if (Convert.ToString(data).CompareTo("UP") == 0) SendMessageW(hwnd, WM_APPCOMMAND, hwnd, (IntPtr)APPCOMMAND_VOLUME_UP);
if (Convert.ToString(data).CompareTo("DN") == 0) SendMessageW(hwnd, WM_APPCOMMAND, hwnd, (IntPtr)APPCOMMAND_VOLUME_DOWN);
if (Convert.ToString(data).CompareTo("PR") == 0) SendMessageW(hwnd, WM_APPCOMMAND, hwnd, (IntPtr)APPCOMMAND_MEDIA_PREVIOUSTRACK);
if (Convert.ToString(data).CompareTo("NE") == 0) SendMessageW(hwnd, WM_APPCOMMAND, hwnd, (IntPtr)APPCOMMAND_MEDIA_NEXTTRACK);
// Process the data sent by the client.
//device.AudioEndpointVolume.MasterVolumeLevelScalar = (Convert.ToInt64(data) / 100.0f);
//byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
//byte[] msg = System.Text.Encoding.ASCII.GetBytes("Successfully set to " + data);
// Send back a response.
//stream.Write(msg, 0, msg.Length);
//Console.WriteLine("Sent: {0}", data);
}
// Shutdown and end connection
client.Close();
}
}
What needs to happen to connect to the server(client code)
Code:
static void Connect(String server, String message)
{
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 13000;
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
MessageBox.Show("ArgumentNullException: " + e.ToString());
}
catch (SocketException e)
{
MessageBox.Show("SocketException: " + e.ToString());
}
}
So I would call Connect("192.168.1.40", "PP"); to pause/play the server(desktop running Zune)
Code:
package com.pwn.control;
import android.app.Activity;
import java.io.*;
import java.net.*;
import android.widget.*;
import android.os.Bundle;
import android.os.StrictMode;
public class ControlActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
tv.setText("HELLO WORLD");
setContentView(tv);
run();
}
TextView tv;
public void run()
{
new Thread(new Runnable() { public void run() {
Socket socket;
try
{
InetAddress serverAddr = InetAddress.getByName("192.168.1.40");
socket = new Socket("192.168.1.40", 13000);
//socket.connect();
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeBytes("PP");
//PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
//out.println("PP");
socket.close();
}
catch (Exception e)
{
tv.setText(e.toString());
//setContentView(R.layout.main);
}
}
} ).start();
}
}
Unfortunately the above code doesn't work. Kinda stuck lol... maybe someone knows more about writing android apps than I do.
http://www.youtube.com/watch?v=PMjNrd1d4FM
Got it working with an ASP site...
now the annoying part... I tried setting it up with a default IIS instance and it doesn't have permissions to use the user32.dll!!! I tried forced impersonation and tons of different tricks but for some reason it isn't getting as high permissions as the ASP.NET debugging server.
So I need to either fix the android app so it will communicate with the service, or I need to find a way to get the IIS instance enough permissions to interact with the desktop. I did set the IIS Admin service to "interact with desktop" but nothing happened.
I also tried setting up Apache 2.2 with mod_asp installed but it has the same result... blocked from interacting.
Ok I got it working but it's really really makeshift right now...
ASP website --> loopback on port 13000 --> C# app(that will actually interface with the Zune software)
I couldn't make the API call from the C# code in the ASP site because IIS doesn't have enough permissions. So since my only drawback before was that I couldn't communicate between .NET TcpListener and Java, I can just use the ASP site to make the TcpClient connection.
The good thing is you can access this interface from anything with a web browser. Just make http://computer-ip:port/ZuneControl a favorite on any device and you can control Zune from it.
http://www.youtube.com/watch?v=haVLCOY0l6U
If you're really eager to try the alpha build with IIS that's fine...
Just set up IIS like I do in the video and add port 13000 to your inbound and outbound firewall rule. I'll work on the UI when I get some time next weekend.
Here is the code for the C# app. http://pastebin.com/08kCjKQW
The web code is in the RAR file. I just copied and pasted out of that pastebin with some extra buttons.
http://tunerspotter.com/\dropbox\misc\ZuneControl.rar
In that ZuneControl folder, ZuneControl.exe is the app. Click start, then minimize it after you set up IIS. It will work for Apache installations also. I have Apache on port 82. http://sourceforge.net/projects/mod-aspdotnet/
Instruction video: http://www.youtube.com/watch?v=ClCQhmQxC7Q
Well i've been using the web interface for a few days and it kicks ass. Does exactly what i want. I can be in bed and change songs/volume from another tab in Opera.
Next weekend Ill see if i can get the program to listen on port 81 as a web service so instead of setting up IIS or apache all you have to do is open the app and click start(then minimize it)
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
hey are you still working on this? I would really love something like this!
well i gave up on the app and just turned it into a ASP site + windows application. so yeah i've been using it for a few months. works great, i can pull up on any web capable device and adjust my music. Whether it's my zune, tablet, computer, or phone, i can adjust volume, go back/next, and pause/play from any device. i set up port forwarding with dyndns.org so it works over 3G

When is CM7 coming out?

Well, now that I have your attention, my REAL question is this: Is there a way to make apps like the Amazon marketplace think they're on wifi so I don't get that stupid "you can't download this because it's too big for a cellular network"?
***** please, I pay for unlimited data and I plan to use it.
Sent from my Droid Charge running Infinity Beta
HAHAHA I like your tactics for getting attention.
A while back I made a remark that the only way to get peoples attention in these forums is to ask if CM7 is coming out in the title and then ask the real question in the body.
Well played
Also no, there's no way to trick Amazon
I've actually been working on figuring out a way to do this.
Any app that tries to figure out how it's connected queries the same class in the system called NetworkInfo.class.
There are two methods within NetworkInfo.class that report the network type: getType() and getTypeName(). getType() returns a machine-readable answer, while getTypeName() is human-readable.
Code:
public int getType() {
return mNetworkType;
}
Code:
public String getTypeName() {
switch (mNetworkType) {
case ConnectivityManager.TYPE_WIFI:
return "WIFI";
case ConnectivityManager.TYPE_MOBILE:
return "MOBILE";
default:
return "<invalid>";
}
}
I haven't had the resources (primarily time) to dig that far into things, but if we could figure out a way to inject something between Amazon and the getType() or getTypeName() calls, we could report to it that we're on WiFi regardless of how we're actually connected.
AlexDeGruven said:
I've actually been working on figuring out a way to do this.
Any app that tries to figure out how it's connected queries the same class in the system called NetworkInfo.class.
There are two methods within NetworkInfo.class that report the network type: getType() and getTypeName(). getType() returns a machine-readable answer, while getTypeName() is human-readable.
Code:
public int getType() {
return mNetworkType;
}
Code:
public String getTypeName() {
switch (mNetworkType) {
case ConnectivityManager.TYPE_WIFI:
return "WIFI";
case ConnectivityManager.TYPE_MOBILE:
return "MOBILE";
default:
return "";
}
}
I haven't had the resources (primarily time) to dig that far into things, but if we could figure out a way to inject something between Amazon and the getType() or getTypeName() calls, we could report to it that we're on WiFi regardless of how we're actually connected.
Click to expand...
Click to collapse
Wow, Computer Science II is actually helping me be able to read this. So, what type of object is mNetworkType and where is it defined? We could get it to just return that its on wifi all the time. Also, do you know what Amazon calls? GetType or GetTypeName?
Sent from my Droid Charge running Infinity Beta
Lol. Nicely done with the thread title.
Sent from my mobile office.
AlexDeGruven said:
I've actually been working on figuring out a way to do this.
Any app that tries to figure out how it's connected queries the same class in the system called NetworkInfo.class.
There are two methods within NetworkInfo.class that report the network type: getType() and getTypeName(). getType() returns a machine-readable answer, while getTypeName() is human-readable.
Code:
public int getType() {
return mNetworkType;
}
Code:
public String getTypeName() {
switch (mNetworkType) {
case ConnectivityManager.TYPE_WIFI:
return "WIFI";
case ConnectivityManager.TYPE_MOBILE:
return "MOBILE";
default:
return "<invalid>";
}
}
I haven't had the resources (primarily time) to dig that far into things, but if we could figure out a way to inject something between Amazon and the getType() or getTypeName() calls, we could report to it that we're on WiFi regardless of how we're actually connected.
Click to expand...
Click to collapse
kvswim said:
Wow, Computer Science II is actually helping me be able to read this. So, what type of object is mNetworkType and where is it defined? We could get it to just return that its on wifi all the time. Also, do you know what Amazon calls? GetType or GetTypeName?
Sent from my Droid Charge running Infinity Beta
Click to expand...
Click to collapse
Interesting stuff. There's an iPhone app for jailbroken phones that performs this function on an app-by-app basis. With this code identified, something similar might be possible on Android. I haven't done any Android dev yet, so I don't know how much help I might be. Is there a way to easily (and cheaply) intercept method calls on Android? If so, there might be a way to intercept the getType and getTypeName calls and then modify them on a case-by-case basis so that the call can be diverted to a different function. I'm talking completely theoretical here...I don't know what is offered by the Android SDK.
shrike1978 said:
Interesting stuff. There's an iPhone app for jailbroken phones that performs this function on an app-by-app basis. With this code identified, something similar might be possible on Android. I haven't done any Android dev yet, so I don't know how much help I might be. Is there a way to easily (and cheaply) intercept method calls on Android? If so, there might be a way to intercept the getType and getTypeName calls and then modify them on a case-by-case basis so that the call can be diverted to a different function. I'm talking completely theoretical here...I don't know what is offered by the Android SDK.
Click to expand...
Click to collapse
That was exactly where my investigation has hit the wall at the moment (and then I got busy finishing my basement).
There are a couple of ways to do it, really. Someone could modify the appstore apk to wrap any calls to getType() and getTypeName(), but that would only be on that particular apk. IIRC, the market does it for large apk downloads as well.
Other apps also look at what your network type is.
I'd love to have something that allows me to toggle what the applications see, regardless of the actual state (I'm thinking of things like Verizon's Skype, etc).
The problem with all of this is that NetworkInfo.class is deep in the core OS, so intercepting any calls to it's methods might be rather difficult at best.
Edit: Also - Can we change the thread title now that we know what it's really all about?
ROTFLMFAO! Awesome thread title!
Sent from my SCH-I510 using xda premium
rofl you got me!!! hahahaha... and to answer your question I don't think there is but it'd be nice to know for sure
blazing through on my 4G Droid Charge
In the Amazon Appstore apk, com.amazon.mas.client.framework.net contains a class called NetworkStateManager whose source is:
Code:
package com.amazon.mas.client.framework.net;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class NetworkStateManager
{
private static final int DELAY_DROP_DETECTION = 5000;
private static final String TAG = "NetworkStateManager";
private static final Map<String, WifiManager.WifiLock> wifiLocks = new ConcurrentHashMap();
private final ConnectivityManager connectivityManager;
private final WeakReference<Context> context;
private final Handler delayHandler;
private final List<NetworkStateListener> listeners = new ArrayList();
private boolean networkDropDetected = false;
private NetworkStateReceiver receiver = null;
private boolean wasConnected = false;
private final WifiManager wifiManager;
public NetworkStateManager(Context paramContext)
{
this.context = new WeakReference(paramContext);
this.wifiManager = ((WifiManager)paramContext.getSystemService("wifi"));
this.connectivityManager = ((ConnectivityManager)paramContext.getSystemService("connectivity"));
this.delayHandler = new NetworkStateHandler(null);
if (isNetworkConnected());
for (boolean bool = false; ; bool = true)
{
this.networkDropDetected = bool;
return;
}
}
private WifiManager.WifiLock getWifiLock(String paramString)
{
if (wifiLocks.containsKey(paramString));
for (WifiManager.WifiLock localWifiLock = (WifiManager.WifiLock)wifiLocks.get(paramString); ; localWifiLock = this.wifiManager.createWifiLock(paramString))
return localWifiLock;
}
public boolean acquireWifiLock(String paramString)
{
WifiManager.WifiLock localWifiLock = getWifiLock(paramString);
if (localWifiLock.isHeld())
Log.w("NetworkStateManager", "Wifi lock identified by " + paramString + " already acquired");
for (int i = 0; ; i = 1)
{
return i;
localWifiLock.acquire();
wifiLocks.put(paramString, localWifiLock);
}
}
public void addListener(NetworkStateListener paramNetworkStateListener)
{
if (!this.listeners.contains(paramNetworkStateListener))
this.listeners.add(paramNetworkStateListener);
}
public void clearListeners()
{
this.listeners.clear();
}
public boolean isNetworkConnected()
{
NetworkInfo localNetworkInfo = this.connectivityManager.getActiveNetworkInfo();
if ((localNetworkInfo == null) || (!localNetworkInfo.isConnected()));
for (int i = 0; ; i = 1)
return i;
}
public boolean isNetworkWifi()
{
return this.connectivityManager.getNetworkInfo(1).isConnected();
}
public boolean isWifiLockAcquired(String paramString)
{
WifiManager.WifiLock localWifiLock = getWifiLock(paramString);
if ((localWifiLock != null) && (localWifiLock.isHeld()));
for (int i = 1; ; i = 0)
return i;
}
public void releaseWifiLock(String paramString)
{
WifiManager.WifiLock localWifiLock = getWifiLock(paramString);
if (localWifiLock.isHeld())
{
localWifiLock.release();
if (wifiLocks.containsKey(paramString))
wifiLocks.remove(paramString);
}
while (true)
{
return;
Log.w("NetworkStateManager", "Wifi lock identified by " + paramString + " is not acquired");
}
}
public void removeListener(NetworkStateListener paramNetworkStateListener)
{
this.listeners.remove(paramNetworkStateListener);
}
public void startListening()
{
Context localContext = (Context)this.context.get();
if (this.receiver != null)
Log.w("NetworkStateManager", "Already listening, duplicate call to NetworkStateManager#startListening");
while (true)
{
return;
if (localContext == null)
{
Log.w("NetworkStateManager", "Call to NetworkStateManager#startListening on null context");
continue;
}
this.wasConnected = isNetworkConnected();
IntentFilter localIntentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
this.receiver = new NetworkStateReceiver(null);
localContext.registerReceiver(this.receiver, localIntentFilter);
}
}
public void stopListening()
{
Context localContext = (Context)this.context.get();
if (this.receiver == null)
Log.w("NetworkStateManager", "Not listening, invalid call to NetworkStateManager#stopListening");
while (true)
{
return;
if (localContext == null)
{
Log.w("NetworkStateManager", "Call to NetworkStateManager#stopListening on null context");
continue;
}
localContext.unregisterReceiver(this.receiver);
this.receiver = null;
this.delayHandler.removeMessages(65536);
}
}
private class NetworkStateHandler extends Handler
{
public static final int MSG_NETWORK_DROP = 65536;
private NetworkStateHandler()
{
}
public void handleMessage(Message paramMessage)
{
switch (paramMessage.what)
{
default:
super.handleMessage(paramMessage);
case 65536:
}
while (true)
{
return;
NetworkStateManager.this.wasConnected = false;
NetworkStateManager.this.networkDropDetected = true;
Iterator localIterator = NetworkStateManager.this.listeners.iterator();
while (localIterator.hasNext())
((NetworkStateManager.NetworkStateListener)localIterator.next()).onConnectivityLost();
}
}
}
public static abstract interface NetworkStateListener
{
public abstract void onConnectivityLost();
public abstract void onConnectivityRestored();
}
private class NetworkStateReceiver extends BroadcastReceiver
{
private NetworkStateReceiver()
{
}
public void onReceive(Context paramContext, Intent paramIntent)
{
if ((paramContext == null) || (paramIntent == null) || (!"android.net.conn.CONNECTIVITY_CHANGE".equals(paramIntent.getAction())));
while (true)
{
return;
if (NetworkStateManager.this.isNetworkConnected());
for (int i = 0; ; i = 1)
{
boolean bool = paramIntent.getBooleanExtra("isFailover", false);
if ((i == 0) || (bool) || (!NetworkStateManager.this.wasConnected))
break label99;
Message localMessage = NetworkStateManager.this.delayHandler.obtainMessage(65536);
NetworkStateManager.this.delayHandler.sendMessageDelayed(localMessage, 5000L);
break;
}
label99: if (i != 0)
continue;
if (NetworkStateManager.this.delayHandler.hasMessages(65536))
NetworkStateManager.this.delayHandler.removeMessages(65536);
if (!NetworkStateManager.this.networkDropDetected)
continue;
NetworkStateManager.this.wasConnected = true;
Iterator localIterator = NetworkStateManager.this.listeners.iterator();
while (localIterator.hasNext())
((NetworkStateManager.NetworkStateListener)localIterator.next()).onConnectivityRestored();
}
}
}
}
I'm guessing that making isNetworkWifi() return true would allow for downloads of large files over the cell network. I might repack the APK and post it here if I find the time.
All right, I decompiled the dex file into smali class files, made the change, recompiled, and produced an APK that does not have the wifi restriction, but I'm having problems with signing. I ran a signing tool on the APK, but even after that my phone still fails to install the app (I do have the previous version of the app uninstalled). Has anyone else had experience with this?
substanceD said:
All right, I decompiled the dex file into smali class files, made the change, recompiled, and produced an APK that does not have the wifi restriction, but I'm having problems with signing. I ran a signing tool on the APK, but even after that my phone still fails to install the app (I do have the previous version of the app uninstalled). Has anyone else had experience with this?
Click to expand...
Click to collapse
Care to post it?
Sent from my Droid Charge running Infinity Beta
kvswim said:
Care to post it?
Sent from my Droid Charge running Infinity Beta
Click to expand...
Click to collapse
Yeah, sure. (For anyone else attempting to download this, it will not install in its current form).
Anyone have any luck with the signing?
AlexDeGruven said:
Anyone have any luck with the signing?
Click to expand...
Click to collapse
I'm having troubles too. Is there any way to bypass the sign check? I assume its similar to a CRC or MD5 check.
EDIT: http://developer.android.com/guide/publishing/app-signing.html
What if you saved & signed as a different app name?
Sent from my Droid Charge running Infinity Beta
Bumping
Sent from my Droid Charge running Infinity Beta
Just had an idea. What if we packed it into a ROM as a system APK?
Sent from my Droid Charge running Infinity Beta
Screw CM7. Why was CM8 skipped?! >:[
Sent from my SCH-I510 using xda premium
DirgeExtinction said:
Screw CM7. Why was CM8 skipped?! >:[
Sent from my SCH-I510 using xda premium
Click to expand...
Click to collapse
Great job reading the thread and making a relevant comment.
Aside from that: CM8 would be Honeycomb-based if it were to ever be made, which is unlikely, since it's the one version of Android that's closed-source. ICS will give rise to CM9
AlexDeGruven said:
Great job reading the thread and making a relevant comment.
Aside from that: CM8 would be Honeycomb-based if it were to ever be made, which is unlikely, since it's the one version of Android that's closed-source. ICS will give rise to CM9
Click to expand...
Click to collapse
Why, thank you.
I was bored and just posted that. I knew this thread was about the Amazon appstore(had read some posts from the first page a few days ago).
Sent from my SCH-I510 using xda premium

[Q] WP7 facebook application, posting blank status updates only

Hi ive written a simple application for windows phone 7 that posts a status update to the users wall. This is all working correctly except for the fact that when the status update is done it is always empty. I am trying to make it so that the user can enter what they would like to say in a text box, but obviously so far this isnt working and all that is being sent is an empty status update. I am connecting to the facebook api and the authentication all works correctly or the post would not be sent at all. Ive searched all over the web and havent found anyone with the same issue as of yet.
Here is the status update code that I am working on:
Code:
private void PostStatusUpdate(string status, Action<bool, Exception> callback)
{
var request = HttpWebRequest.Create("https://graph.facebook.com/me/feed");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream((reqResult) =>
{
using (var strm = request.EndGetRequestStream(reqResult))
using (var writer = new StreamWriter(strm))
{
writer.Write("access_token=" + _accessToken);
writer.Write("&message=" + HttpUtility.UrlEncode(status));
}
request.BeginGetResponse((result) =>
{
try
{
var response = request.EndGetResponse(result);
using (var rstrm = response.GetResponseStream())
{
var serializer = new DataContractJsonSerializer(typeof(FacebookPostResponse));
var postResponse = serializer.ReadObject(rstrm) as FacebookPostResponse;
callback(true, null);
}
}
catch (Exception ex)
{
callback(false, ex);
}
}, null);
}, null);
}
[DataContract]
public class FacebookPostResponse
{
[DataMember(Name = "id")]
public string Id
{
get;
set;
}
}
private void PostUpdate_Click(object sender, RoutedEventArgs e)
{
PostStatusUpdate(this.StatusText.Text, (success, ex) =>
{
this.Dispatcher.BeginInvoke(() =>
{
if (success && ex == null)
{
MessageBox.Show("Status updated");
NavigationService.Navigate(new Uri("/Views/MainPage.xaml", UriKind.Relative));
}
else
{
MessageBox.Show("Unable to update status");
}
});
});
}
Has anyone encountered this before / know any solution to this problem?
Any insights into this would be much appreciated
You are using the asyncronous method with no callback function
Looks like you should be using a callback function - See examples here
http://msdn.microsoft.com/en-us/lib...est.begingetrequeststream(v=vs.90).aspx#Y1760
http://msdn.microsoft.com/en-us/lib...brequest.begingetrequeststream(v=vs.100).aspx
Also, did the same code work at one point?
It could be as simple as not closing your connections. I ran into this in the Weather City Editor that I wrote for Windows Mobile.
When I added code to hit accuweathers site to look up the code, I had forgotten to close the connection. I would need to wait until the connection timed out before it would work again.
I think yours is differnt though, since it is updating blank.
But, you could just use GetRequestStream, which does not need the callback function, synce it is not asyncronous. See example code: http://msdn.microsoft.com/en-us/library/d4cek6cc.aspx
Thanks for the reply
Thanks for the reply, the code has never worked so far it has only posted blank facebook statuses, I will see if adding an asynchronous call back makes a difference.
Thanks

How to Read PDF file Without save on Local storage windows phone 8 C#

I need to open pdf file without store on local storage. i tried this code but i am facing some issues when i am try to read pdf file through PDF Reader, Adobe Reader, etc. after click back navigation i deleted current file from local folder. But after closed my application and I am directly open PDF Reader, Adobe Reader... etc. File is exist(still have on my library). I could not delete the file. Please let me know is possible on windows phone 8 C#.
please see my code below.
Code:
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
try
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var fileStream = store.OpenFile("your-file.pdf", FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
fileStream.Flush();
fileStream.Close();
if (store.FileExists("your-file.pdf"))
{
store.DeleteFile("your-file.pdf");
}
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
await local.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
}
}
catch
{
//File does not exist.
}
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
//RestSharp from Nuget
var client = new RestClient("https://dvtp17lb47b1q.cloudfront.net");
var request = new RestRequest("/magazine/file/PDF-2.pdf?c34tP5IxEK=2b26c756615a40844a6564d13a3eb875", Method.GET);
var response = await client.ExecuteTaskAsync(request);
byte[] buffer = response.RawBytes;
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = storageFile.OpenFile("your-file.pdf", FileMode.Create))
{
await stream.WriteAsync(buffer, 0, buffer.Length);
}
}
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile pdffile = await local.GetFileAsync("your-file.pdf");
// Launch the pdf file.
Windows.System.Launcher.LaunchFileAsync(pdffile);
}
Main Functionality is only read the pdf file without saving.

Categories

Resources