Am Generating 3 tabs dynamically under 1st tab am generating a toggle button using which i can switch off and on both GPS and WIFI
I have coded java files for GPS and WIFI. I have checked again and again line by line but couldn't find any bug.
I have run in the emulator and android "Sony Arc mobile also."Installation also goes well and problem arises when i try to launch.
The application is showing the message "The application has stopped unexpectedly please try again" I have referred almost{all} all the queries in stack overflow and couldn't get any possible answer for my scenario..
I tried removing the GPS and WIFI files in the application and replacing Toggle Button with TextView then its working fine.
Am including my code please verify it and help me out buddies.
Main.java
package project.sow;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class Main extends TabActivity {
private TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
tabHost = (TabHost)findViewById(android.R.id.tabhost); //The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Start").setIndicator("Start", res.getDrawable(R.drawable.ic_sample));
spec.setContent(new Intent(this,Start.class));
tabHost.addTab(spec);
spec = tabHost.newTabSpec("Info").setIndicator("Info", res.getDrawable(R.drawable.ic_sample));
spec.setContent(new Intent().setClass(this, Info.class));
tabHost.addTab(spec);
spec = tabHost.newTabSpec("Update").setIndicator("Update", res.getDrawable(R.drawable.ic_sample));
spec.setContent(new Intent(this, Update.class));
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}catch(Exception e)
{
android.util.Log.i("PREFS",e.getMessage());
} } }
Start.java
package project.sow;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class Start extends Activity {
public WifiManager wifi;
boolean status;
Context context;
public TextView textStatus;
public LocationManager locationManager;
public LocationListener locationListener;
public BroadcastReceiver receiver;
public LocationProvider locationProvider;
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Toggle to On r Off the Application");
setContentView(textview);
ToggleButton btnonoff = new ToggleButton(this);
setContentView(btnonoff);
if(btnonoff.isChecked()){
status=true;
wifi.setWifiEnabled(true);
try{
// Setup WiFi
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (receiver == null)
receiver = new WiFiFilter(this);
registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
if (locationListener ==null ){locationListener=new gpsLocation(this);}
}
catch(Exception e){Toast.makeText(context, WIFI_SERVICE, Toast.LENGTH_LONG).show();}
}
else if(!btnonoff.isChecked()) {
status=false;
if(wifi.isWifiEnabled()){
wifi.setWifiEnabled(false);
locationManager.removeUpdates((LocationListener) this);
}
}
}
catch(Exception e)
{
android.util.Log.i("PREFS",e.getMessage());
}
} }
WifiFilter.java
package project.sow;
import java.util.List;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;
public class WiFiFilter extends BroadcastReceiver {
private static final String TAG = "WiFiFilter";
Start start;
public WiFiFilter(Start start) {
super();
this.start=start;
}
//Preparing for Hidden SSID and saving the wifi config for later use
@Override
public void onReceive(Context c, Intent intent) {
List results = start.wifi.getScanResults();
List results2 = null;
ScanResult filter = null;
//filtering SSID
for(ScanResult filterresult : results ){
if(filterresult.SSID=="VOLSBB")
{
results2 = results;}
}
//Checking the signal Level
for (ScanResult result : results2) {
if (filter == null
|| WifiManager.compareSignalLevel(filter.level, result.level) < 0)
filter = result;
try{
WifiManager.ACTION_PICK_WIFI_NETWORK.equals(filter);
}
catch(Exception e){String res="Exception:No network";Toast.makeText(start, res, Toast.LENGTH_LONG).show();};
}
String message = String.format("Connected to S0W");
if(filter.SSID != null){
//Toast.makeText(start, message, Toast.LENGTH_LONG).show();
Log.d(TAG, "onReceive() message: " + message);
}
} }
gpsLocation.java
package project.sow;
import project.sow.CustomWebView;
import android.location.Criteria;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import project.sow.Start;
public class gpsLocation implements LocationListener{
Start sowWifigps;
public Location currentBestLocation;
public Location location;
GpsStatus status;
CustomWebView webviewgps;
double a;
double b;
@SuppressWarnings("unused")
private String bestProvider;
//public LocationManager locationManager;
public String provider1;
public gpsLocation(Start sowWifigps) {
super();
this.sowWifigps=sowWifigps;
// TODO Auto-generated constructor stub
Criteria criteria = new Criteria();
//bestProvider = sowWifigps.locationManager.getBestProvider(criteria, false);
try{
if(LocationManager.GPS_PROVIDER==null){
provider1 = LocationManager.NETWORK_PROVIDER;
}
else {provider1=LocationManager.GPS_PROVIDER;}
}catch( Exception e){ provider1=sowWifigps.locationManager.getBestProvider(criteria, false);}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
sowWifigps.locationManager.requestLocationUpdates(provider1, 12000, 200, sowWifigps.locationListener);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
//provider = LocationManager.NETWORK_PROVIDER;
currentBestLocation=sowWifigps.locationManager.getLastKnownLocation(provider1);
a=currentBestLocation.getLatitude();
b=currentBestLocation.getLongitude();
webviewgps.loadUrl("http://localhost/mobile/default_mobile.php?lat="+a+"&long="+b); //change to site name
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
sowWifigps.locationManager.requestLocationUpdates(provider1, 12000, 200, sowWifigps.locationListener);
a=currentBestLocation.getLatitude();
b=currentBestLocation.getLongitude();
webviewgps.loadUrl("http://localhost/mobile/default_mobile.php?lat="+a+"&long="+b); //change to site name
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
sowWifigps.locationListener.onLocationChanged(location);
}
}
Am presenting here second Tab and other resembles this one.So am omitting it.
Info.java
package project.sow;
import android.app.Activity;
import android.os.Bundle;
public class Info extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
CustomWebView webView = new CustomWebView(this);
webView.loadUrl("http://www.google.com/?s=Info");
setContentView(webView);
super.onResume();
} }
CustomWebView.java
package project.sow;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class CustomWebView extends WebView {
private ProgressDialog pgBar = null;
@SuppressWarnings("unused")
private Context ctx;
public CustomWebView(Context context) {
super(context);
this.clearCache(true);
this.clearFormData();
this.clearHistory();
this.getSettings().setJavaScriptEnabled(true);
this.getSettings().setUserAgentString(null);
this.requestFocus(View.FOCUS_DOWN);
this.setWebViewClient(new CustomWebView.CustomWebViewClient());
this.pgBar = new ProgressDialog(context);
this.pgBar.setMessage("Loading...");
ctx = context;
}
private class CustomWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (!CustomWebView.this.pgBar.isShowing()) {
CustomWebView.this.pgBar.show();
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (CustomWebView.this.pgBar.isShowing()) {
CustomWebView.this.pgBar.dismiss();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try{
view.loadUrl(url);
}catch(Exception ex){
}
return true;
} } }
main.XML
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com /apk/res/android" android:id="@android:id/tabhost" android:background="#cabfa9">
<LinearLayout android:id="@+id/LinearLayout01"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" android:background="#1b3b5b" ></TabWidget>
<FrameLayout android:id="@android:id/tabcontent" android:layout_height="fill_parent" android:layout_width="fill_parent" android:scrollbars="horizontal" android:background="#cabfa9">
</FrameLayout>
</LinearLayout>
</TabHost>
style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="tabText" parent="@android:style/Theme.Black">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">10dip</item>
</style>
</resources>
`
ic_sample
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/icon1"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/icon2" />
</selector>
I have added the following permissions in the manifest file
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />