please help me in this code

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

please help me in this code

Postby HaMaNeY » Mon May 25, 2009 1:04 am

Hi guys,
I want to create an application that when I send a message to the phone it turns wifi on and send a message back to me with ip address. When I made the program an error appeared in the Manifest.xml and I don't know how to fix it. Can anyone help me please? the code is shown below:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.net.InetAddress;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.content.BroadcastReceiver;
  8.  
  9. import android.content.Context;
  10.  
  11. import android.content.Intent;
  12.  
  13. import android.net.wifi.WifiInfo;
  14.  
  15. import android.net.wifi.WifiManager;
  16.  
  17. import android.os.Bundle;
  18.  
  19. import android.telephony.gsm.SmsManager;
  20.  
  21. import android.telephony.gsm.SmsMessage;
  22.  
  23. import android.widget.Toast;
  24.  
  25.  
  26.  
  27. public class IpApplication extends BroadcastReceiver
  28.  
  29. {
  30.  
  31.         private WifiManager wifiManager;
  32.  
  33.         private WifiInfo wifi;
  34.  
  35.         Object info;
  36.  
  37.     public void onReceive(Context context, Intent intent)
  38.  
  39.     {
  40.  
  41.        
  42.  
  43.         try{
  44.  
  45.                 //---get the SMS message passed in---
  46.  
  47.        
  48.  
  49.         Bundle bundle = intent.getExtras();        
  50.  
  51.         SmsMessage[] msgs = null;
  52.  
  53.         String str = "";  
  54.  
  55.         String number="";
  56.  
  57.         if (bundle != null)
  58.  
  59.         {
  60.  
  61.             //---retrieve the SMS message received---
  62.  
  63.             Object[] pdus = (Object[]) bundle.get("pdus");
  64.  
  65.             msgs = new SmsMessage[pdus.length];            
  66.  
  67.             for (int i=0; i<msgs.length; i++){
  68.  
  69.                 msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
  70.  
  71.                 number +=msgs[i].getOriginatingAddress();                    
  72.  
  73.                
  74.  
  75.                 str += msgs[i].getMessageBody().toString();
  76.  
  77.                        
  78.  
  79.             }
  80.  
  81.             //---display the new SMS message---
  82.  
  83.             //Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
  84.  
  85.            
  86.  
  87.             if(str.equals("1")){
  88.  
  89.            
  90.  
  91.            Thread x = new Thread(new runsms(number));
  92.  
  93.            x.start();
  94.  
  95.                
  96.  
  97.                 /*     
  98.  
  99.            
  100.  
  101.                 //enable wifi and get ip address
  102.  
  103.             wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  104.  
  105.            
  106.  
  107.             wifiManager.setWifiEnabled(true);
  108.  
  109.            
  110.  
  111.             InetAddress addr = InetAddress.getLocalHost();
  112.  
  113.             byte[] ipAddr = addr.getAddress();
  114.  
  115.        
  116.  
  117.             // Convert to dot representation
  118.  
  119.             String ipAddrStr = "";
  120.  
  121.             for (int i=0; i<ipAddr.length; i++) {
  122.  
  123.                 if (i > 0) {
  124.  
  125.                     ipAddrStr += ".";
  126.  
  127.                 }
  128.  
  129.                 ipAddrStr += ipAddr[i]&0xFF;
  130.  
  131.             }
  132.  
  133.  
  134.  
  135.             //reply msg
  136.  
  137.             SmsManager smsMgr = SmsManager.getDefault();
  138.  
  139.             String sendTo = number;
  140.  
  141.             String myMessage = ipAddrStr;
  142.  
  143.             smsMgr.sendTextMessage(sendTo, null, myMessage, null, null);
  144.  
  145.             */
  146.  
  147.             }
  148.  
  149.         }                        
  150.  
  151.    
  152.  
  153.     }
  154.  
  155.     catch(Exception e){
  156.  
  157.         e.printStackTrace();
  158.  
  159.     }
  160.  
  161. }
  162.  
  163. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.net.InetAddress;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.content.Context;
  8.  
  9. import android.net.wifi.WifiInfo;
  10.  
  11. import android.net.wifi.WifiManager;
  12.  
  13. import android.os.Bundle;
  14.  
  15. import android.telephony.gsm.SmsManager;
  16.  
  17.  
  18.  
  19. public class runsms extends Activity implements Runnable{
  20.  
  21.         private WifiManager wifiManager;
  22.  
  23.         private WifiInfo wifi;
  24.  
  25.         Object info;
  26.  
  27.         private String number="";
  28.  
  29.         public runsms(String x){
  30.  
  31.                 number+=x;
  32.  
  33.         }
  34.  
  35.         public void run() {
  36.  
  37.         try{
  38.  
  39.         //@Override
  40.  
  41.     //public void onCreate(Bundle savedInstanceState) {
  42.  
  43.       //  super.onCreate(savedInstanceState);
  44.  
  45.        
  46.  
  47.         try{
  48.  
  49.         //enable wifi and get ip address
  50.  
  51.     wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  52.  
  53.    
  54.  
  55.     wifiManager.setWifiEnabled(true);
  56.  
  57.    
  58.  
  59.     InetAddress addr = InetAddress.getLocalHost();
  60.  
  61.     byte[] ipAddr = addr.getAddress();
  62.  
  63.  
  64.  
  65.     // Convert to dot representation
  66.  
  67.     String ipAddrStr = "";
  68.  
  69.     for (int i=0; i<ipAddr.length; i++) {
  70.  
  71.         if (i > 0) {
  72.  
  73.             ipAddrStr += ".";
  74.  
  75.         }
  76.  
  77.         ipAddrStr += ipAddr[i]&0xFF;
  78.  
  79.     }
  80.  
  81.  
  82.  
  83.     //reply msg
  84.  
  85.     SmsManager smsMgr = SmsManager.getDefault();
  86.  
  87.     String sendTo = number;
  88.  
  89.     String myMessage = ipAddrStr;
  90.  
  91.     smsMgr.sendTextMessage(sendTo, null, myMessage, null, null);
  92.  
  93.         }
  94.  
  95.         catch(Exception e){
  96.  
  97.                 e.printStackTrace();
  98.  
  99.         }
  100.  
  101.         //}
  102.  
  103.         }
  104.  
  105.         catch(Exception e){
  106.  
  107.                 e.printStackTrace();
  108.  
  109.         }
  110.  
  111.         }
  112.  
  113. }
  114.  
  115.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.      package="Ip.and"
  6.  
  7.      android:versionCode="1"
  8.  
  9.      android:versionName="1.0.0">
  10.  
  11.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  12.  
  13.         <activity android:name=".IpApplication"
  14.  
  15.                  android:label="@string/app_name">
  16.  
  17.             <intent-filter>
  18.  
  19.                 <action android:name="android.intent.action.MAIN" />
  20.  
  21.                 <category android:name="android.intent.category.LAUNCHER" />
  22.  
  23.             </intent-filter>
  24.  
  25.         </activity>
  26.  
  27.     </application>
  28.  
  29. <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
  30.  
  31. <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
  32.  
  33. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
  34.  
  35. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
  36.  
  37. <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
  38.  
  39. </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4
HaMaNeY
Freshman
Freshman
 
Posts: 3
Joined: Wed May 13, 2009 4:16 pm

Top

Postby mrosseel » Mon May 25, 2009 12:04 pm

please provide the error in the manifest file, that might speed up the resolution of your problem.
mrosseel
Developer
Developer
 
Posts: 25
Joined: Sun Sep 21, 2008 11:19 am

Postby HaMaNeY » Mon May 25, 2009 6:04 pm

Thanks mrosseel for your reply.

The error is in this line in the Manifest.xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.   android:label="@string/app_name">
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


and the error is said that ipApplication does not extends activity :?
HaMaNeY
Freshman
Freshman
 
Posts: 3
Joined: Wed May 13, 2009 4:16 pm

Postby mrosseel » Mon May 25, 2009 6:35 pm

If you use this in your manifest:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <activity android:name=".IpApplication"
  3.  
  4.  
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


and you make ipApplication extend BroadcastReceiver (which doesn't extend Activity) I think the compiler has the right to complain.

Make an activity to serve as the entrypoint for your application. How to use your broadcastreceiver I don't know, I'm sure there will be examples around.

Mike
mrosseel
Developer
Developer
 
Posts: 25
Joined: Sun Sep 21, 2008 11:19 am

Postby mrosseel » Mon May 25, 2009 6:37 pm

If you use this in your manifest:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <activity android:name=".IpApplication"
  3.  
  4.  
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


and you make ipApplication extend BroadcastReceiver (which doesn't extend Activity) I think the compiler has the right to complain.

Make an activity to serve as the entrypoint for your application.

Read the javadocs for BroadcastReceiver on how to use it, see below for a small part of it which seems to explain exactly what you need to do to make it work:

Base class for code that will receive intents sent by sendBroadcast(). You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the <receiver> tag in your AndroidManifest.xml. Note: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack.


Mike
mrosseel
Developer
Developer
 
Posts: 25
Joined: Sun Sep 21, 2008 11:19 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 2 guests