0

在第一个选项卡下动态生成 3 个选项卡正在生成一个切换按钮,使用它我可以关闭和打开 GPS 和 WIFI
我已经为 GPS 和 WIFI 编写了 java 文件。我一次又一次地逐行检查,但找不到任何错误。我已经在模拟器和android“Sony Arc mobile”中运行过。安装也很顺利,当我尝试启动时出现问题。应用程序显示消息“应用程序已意外停止,请重试”我已经参考了几乎{所有}堆栈溢出中的所有查询,并且无法为我的场景获得任何可能的答案。我尝试删除 GPS 和 WIFI 文件在应用程序中并用 TextView 替换Toggle Button然后它工作正常。
我包括我的代码,请验证它并帮助我。

主.java

包项目.sow;
导入android.app.TabActivity;
导入android.content.Intent;
导入android.content.res.Resources;
导入android.os.Bundle;
导入 android.widget.TabHost;
公共类 Main 扩展 TabActivity {
私有 TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
设置内容视图(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());  
} } }     

启动.java

包项目.sow;
导入android.app.Activity;
导入 android.content.BroadcastReceiver;
导入android.content.Context;
导入 android.content.IntentFilter;
导入 android.location.LocationListener;
导入 android.location.LocationManager;
导入 android.location.LocationProvider;
导入android.net.wifi.WifiManager;
导入android.os.Bundle;
导入 android.widget.TextView;
导入 android.widget.Toast;
导入 android.widget.ToggleButton;
公共类开始扩展活动{
公共WifiManager wifi;
布尔状态;
上下文上下文;
公共文本视图文本状态;
公共位置管理器位置管理器;
公共位置监听器位置监听器;
公共广播接收器接收器;
公共位置提供者位置提供者;

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

包项目.sow;
导入 java.util.List;
导入 android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.net.wifi.ScanResult;
导入android.net.wifi.WifiManager;
导入android.util.Log;
导入 android.widget.Toast;
公共类 WiFiFilter 扩展 BroadcastReceiver {
private static final String TAG = "WiFiFilter";
开始开始;
公共 WiFiFilter(开始开始) {
super();
this.start=开始;
}
//准备隐藏SSID并保存wifi配置以备后用
@Override
public void onReceive(Context c, Intent intent) {
List results = start.wifi.getScanResults();
列出结果2 = null;
扫描结果过滤器 = null;
//过滤 SSID
for(ScanResult filterresult : results ){
if(filterresult.SSID=="VOLSBB")
{ results2
= results;}
}
//检查信号电平
for (ScanResult result : results2) {
if (filter == null
|| WifiManager.compareSignalLevel(filter.level, result.level) < 0)
filter = result;
尝试{
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("连接到 S0W");
if(filter.SSID != null){
//Toast.makeText(start, message, Toast.LENGTH_LONG).show();
Log.d(TAG, "onReceive() 消息:" + 消息);
}
} }

gpsLocation.java

包项目.sow;
导入项目.sow.CustomWebView;
导入android.location.Criteria;
导入android.location.GpsStatus;
导入android.location.Location;
导入 android.location.LocationListener;
导入 android.location.LocationManager;
导入android.os.Bundle;
导入project.sow.Start;
公共类 gpsLocation 实现 LocationListener{
开始 sowWifigps;
公共位置 currentBestLocation;
公共位置位置;
GpsStatus 状态;
CustomWebView webviewgps;
双a;
双乙;
@SuppressWarnings("未使用")
私有字符串 bestProvider;
//公共位置管理器位置管理器;
公共字符串提供者1;
公共gpsLocation(开始sowWifigps){
超级();
this.sowWifigps=sowWifigps;
// TODO 自动生成的构造函数存根
Criteria criteria = new Criteria();
//bestProvider = sowWifigps.locationManager.getBestProvider(criteria, false);
尝试{
if(LocationManager.GPS_PROVIDER==null){
provider1 = LocationManager.NETWORK_PROVIDER;
}
否则 {provider1=LocationManager.GPS_PROVIDER;}
}catch( Exception e){ provider1=sowWifigps.locationManager.getBestProvider(criteria, false);}
}
@Override
public void onLocationChanged(Location location) {
// TODO 自动生成的方法存根
sowWifigps.locationManager.requestLocationUpdates(provider1, 12000, 200、sowWifigps.locationListener);
}
@Override
public void onProviderDisabled(String provider) {
// TODO 自动生成的方法存根
//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); //更改为站点名称
}
@Override
public void onProviderEnabled(String provider) {
// TODO 自动生成的方法存根
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); //更改为站点名称
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO 自动生成的方法存根
sowWifigps.locationListener.onLocationChanged(location);
}
}

我在这里展示第二个选项卡,其他类似于这个。所以我省略了它。


信息.java

包项目.sow;
导入android.app.Activity;
导入android.os.Bundle;
公共类信息扩展活动 {
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 ");
设置内容视图(网络视图);
超级.onResume();
} }



CustomWebView.java
包 project.sow;
导入 android.app.ProgressDialog;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.view.View;
导入 android.webkit.WebView;
导入 android.webkit.WebViewClient;
公共类 CustomWebView 扩展 WebView {
private ProgressDialog pgBar = null;
@SuppressWarnings("未使用")
私有上下文 ctx;
公共CustomWebView(上下文上下文){
超(上下文);
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("加载中...");
ctx = 上下文;
}
私有类 CustomWebViewClient 扩展 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){
}
返回真;
} } }

主要的.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>

样式.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" />

4

1 回答 1

0

WI-FI 和 GPS 将在主线程而不是子线程中调用。
所以在 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();
   }  
}     

将被放置在 start.java 中以前称为初始化的位置

通过这样做,不会杀死重要的进程并阻止应用程序崩溃。

于 2011-04-29T16:40:40.753 回答