首页 文章

使用Tabs,Wifi和Gps

提问于
浏览
0

在第一个选项卡下动态生成3个选项卡我生成一个切换按钮,使用该按钮可以关闭GPS和WIFI
我为GPS和WIFI编写了java文件 . 我已经一次又一次地检查,但找不到任何错误 . 我已经在模拟器和android "Sony Arc mobile also."运行安装也顺利,当我尝试启动时出现问题 . 应用程序显示消息“ The application has stopped unexpectedly please try again ”我已经在堆栈溢出中引用了几乎所有查询,并且无法为我的场景获得任何可能的答案..我尝试删除应用程序中的GPS和WIFI文件并替换切换按钮使用TextView然后它的工作正常 .
包括我的代码,请验证它并帮助我的好友 .

Main.java包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扩展TabActivity {private TabHost tabHost; @Override public void onCreate(Bundle savedInstanceState){try {super.onCreate(savedInstanceState);的setContentView(R.layout.main);资源res = getResources(); //获取Drawables的资源对象tabHost =(TabHost)findViewById(android.R.id.tabhost); //活动TabHost TabHost.TabSpec规范; //每个选项卡的可重复使用的TabSpec //为每个选项卡初始化TabSpec并将其添加到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包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;布尔状态;上下文背景; public TextView textStatus; public LocationManager locationManager; public LocationListener locationListener;公共BroadcastReceiver接收器; 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包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;公共类WiFiFilter扩展BroadcastReceiver {private static final String TAG =“WiFiFilter”;开始; public WiFiFilter(Start start){super(); this.start =开始; } //准备隐藏的SSID并保存wifi配置供以后使用@Override public void onReceive(Context c,Intent intent){List results = start.wifi.getScanResults();列出results2 = null; ScanResult filter = null; //过滤SSID for(ScanResult filterresult:results){if(filterresult.SSID ==“VOLSBB”)} //检查信号等级(ScanResult result:results2){if(filter == null) || WifiManager.compareSignalLevel(filter.level,result.level)<0)filter = result;尝试{WifiManager.ACTION_PICK_WIFI_NETWORK.equals(过滤器); } catch(Exception e){String res =“Exception:No network”; Toast.makeText(start,res,Toast.LENGTH_LONG).show();}; } String message = String.format(“已连接到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实现LocationListener {Start sowWifigps; public Location currentBestLocation;公共场所位置; GpsStatus状态; CustomWebView webviewgps;双倍;双b; @SuppressWarnings(“unused”)private String bestProvider; // public LocationManager locationManager; public String provider1; public gpsLocation(Start sowWifigps){super(); this.sowWifigps = sowWifigps; // TODO自动生成的构造函数存根条件标准= new Criteria(); // bestProvider = sowWifigps.locationManager.getBestProvider(criteria,false); try {if(LocationManager.GPS_PROVIDER == null) else } catch(异常e)} @Override public void onLocationChanged(Locationlocation){// TODO自动生成的方法stub sowWifigps.locationManager.requestLocationUpdates(provider1,12000,200,sowWifigps.locationListener); } @Override public void onProviderDisabled(String provider){// TODO自动生成的方法stub // provider = LocationManager.NETWORK_PROVIDER; currentBestLocation = sowWifigps.locationManager.getLastKnownLocation(provider1);一个= currentBestLocation.getLatitude(); B = currentBestLocation.getLongitude(); webviewgps.loadUrl(“http://localhost/mobile/default_mobile.php?lat =”a“&long =”b); //更改为站点名称} @Override public void onProviderEnabled(String provider){// TODO自动生成的方法stub sowWifigps.locationManager.requestLocationUpdates(provider1,12000,200,sowWifigps.locationListener);一个= currentBestLocation.getLatitude(); B = currentBestLocation.getLongitude(); webviewgps.loadUrl(“http://localhost/mobile/default_mobile.php?lat =”a“&long =”b); //更改为站点名称} @Override public void onStatusChanged(String provider,int status,Bundle extras){// TODO自动生成的方法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(web视图); super.onResume(); }}

CustomWebView.java包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;公共类CustomWebView扩展WebView {private ProgressDialog pgBar = null; @SuppressWarnings(“unused”)私有上下文ctx; public CustomWebView(Context context){super(context); this.clearCache(真); this.clearFormData(); this.clearHistory(); this.getSettings()setJavaScriptEnabled(真) . this.getSettings()setUserAgentString(空) . this.requestFocus(View.FOCUS_DOWN); this.setWebViewClient(new CustomWebView.CustomWebViewClient()); this.pgBar = new ProgressDialog(context); this.pgBar.setMessage( “载入中...”); ctx = context;私有类CustomWebViewClient扩展了WebViewClient {@Override public void onPageStarted(WebView视图,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){}返回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>

我在清单文件中添加了以下权限

<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" />

1 回答

  • 0

    WI-FI and GPS are to be called in the main threads rather than in child threads..
    所以在main.java中,

    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();
       }  
    }
    

    Are to be placed which are formerly called initialized in the start.java

    通过执行此操作,不会杀死重要的进程并阻止应用程序崩溃 .

相关问题