accessing raw resources in worker thread. I have been trying to use Context.getResources but this seems to create some type of handler error.

by Bob » Fri, 13 Mar 2009 13:31:17 GMT


Sponsored Links
 Hi,
How do I access raw resource via the context
--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Trying to start a thread dedicated to sending and receiving data

Hi,

I have a main class activity that does the task of sending control to
other activities on click of a button. I am also starting a thread in
the main class. This thread basically sets up a connection with the
server and is active throughout the lifetime of the application.
I have two functions in the thread class, send and receive data from
server. Therefore, when the other activities need to send/receive data
from server, they use those particular functions.
First of all please let me know if what I am trying to do is fine as
per the architecture of the operating system. If its fine, when I try
to run the code, the app crashes and i see a NullPointerException and
a RuntimeException in the DDMS log.
Could some one please help me out in this or suggest a better way to
implement the concept.
Also, please be assured that, the other functionality of the code
works perfectly fine.
The main class code is as below
[code]
package com.getfriends;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class GetFriendsActivity extends Activity{


                private MyFriend obj= new MyFriend();
                List<MyFriend> listOfFriends = new ArrayList<MyFriend>();

                int i=0;
                private String FName=null;
                private String LName=null;
                private String Latitude=null;
                private String Longitude=null;
                private String TimeStamp=null;
                public static ArrayList<String> namesArray=new 
ArrayList<String>();

                public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                Thread cThread= null;
                try{
                cThread = new Thread(new ConnectSocket());
                cThread.start();}
                catch (Exception e){
                        Toast.makeText(getBaseContext(),
                            "Unable to start thread",
                            Toast.LENGTH_LONG).show();

                }
                Toast.makeText(getBaseContext(),
                    "Starting",
                    Toast.LENGTH_LONG).show();

                try {
                                getFriendsList();
                        } catch (Exception e) {
                                Toast.makeText(getBaseContext(),
                            "Unable to fetch friend list:"+e.getMessage(),
                            Toast.LENGTH_LONG).show();
e.printStackTrace();
                        }
                        try {
                                displayFriendsList();
                        } catch (Exception e) {
                                Toast.makeText(getBaseContext(),
                            "Unable to display friend list:"+e.getMessage()
+e.getLocalizedMessage(),
                            Toast.LENGTH_LONG).show();
e.printStackTrace();
                        }
                        ConnectSocket socket = new ConnectSocket();

                        socket.sendData("ABCDEF");
                        String k = socket.recieveData();
                        Toast.makeText(getBaseContext(),
                    "Recieved from server:"+k,
                    Toast.LENGTH_LONG).show();



            }


                private void getFriendsList() {


                                        String line[] = new String[3];
                                        line[0]= 
"Abdul#Raqeeb#23.44#34.44#4.45";
                                        line[1]= "Abdul#Azeez#33.44#44.44#5.45";
                                        line[2]= 
"Kiral#Azeez#53.44#454.44#6.45";
                                        for(int k=0;k<=2;k++){
                                        StringTokenizer tokens=new 
StringTokenizer(line[k],"#");
                                    try{
                                        while(tokens.hasMoreTokens())
                                    {
                                        FName=tokens.nextToken();
                                        LName=tokens.nextToken();
                                        Latitude=tokens.nextToken();
                                        Longitude=tokens.nextToken();
                                        TimeStamp=tokens.nextToken();
                                        listOfFriends.add(new
MyFriend(FName,LName,Latitude,Longitude,TimeStamp));
                                    }
                                    }
                                    catch (Exception e){
                                        Toast.makeText(this,
                                                       "Some prob 
here:"+e.getLocalizedMessage(),
                                                            
Toast.LENGTH_SHORT).show();
                                        e.printStackTrace();
                                    }
                                        }


                }


}

[/code]

The code related to the thread is
[code]
package com.getfriends;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;

import android.util.Log;

public class ConnectSocket implements Runnable{
    /** Called when the activity is first created. */


    public String fromServer=null;
    public static BufferedReader inFromServer=null;
    public static Socket clientSocket=null;
    public InetAddress address =null;
    public static DataOutputStream outToServer=null;


    @Override
    public void run(){


                try {
                        address = InetAddress.getByName("127.0.0.1");
                Log.d("TCP", "C: Connecting...");
                clientSocket = new Socket(address, 9999);
                outToServer = new
DataOutputStream(clientSocket.getOutputStream());
                        inFromServer = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
                } catch (Exception e) {
                        System.out.println("Not Connected to the internet");
                }

        }

    public void sendData(String s){
        try {
                if(s.equals(null)) s="hello";
                Log.d("TCP", "C: Sending...");
                        outToServer.writeBytes(s+'#');
                        outToServer.flush();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        Log.d("TCP", "unable to send msg");
                        e.printStackTrace();
                }

    }

    public String recieveData(){
        try {
                        fromServer = inFromServer.readLine();
                        if(fromServer.equals(null)) fromServer="bad luck";
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        Log.d("TCP", "unable to send msg");
                        e.printStackTrace();
                }
                return fromServer;


    }


}
[/code]

Many thanks,
Raqeeb

-- 

2. about late resume, when PowerManagerService write on to /sys/power/state to call late resume

Hi,

Finally I found the answer of this question, and I write a post aboult the
suspend and resume in android, url:
http://www.thinksrc.com/2010/04/7/suspend-en.html
hope this will help others.

thanks.

---
Best regards,
Zhang Jiejing


2010/4/6 ZhangJieJing <kzj...@gmail.com>


-- 

3. which Rom is better for our nexus

4. Just info - What is ROM

5. Sekali2 nyombong

6. Setting up local android repo

7. Cancel a Dialog by clicking on it (no buttons)