使用标签、Wifi 和 Gps
我正在第一个选项卡下动态生成 3 个选项卡正在生成一个切换按钮,我可以使用它来关闭和打开 GPS 和 WIFI
我已经为 GPS 和 WIFI 编写了 java 文件。我逐行检查了一遍又一遍,但找不到任何错误。 我已经在模拟器和android“Sony Arc mobile也”中运行。安装也很顺利,但当我尝试启动时出现问题。 应用程序显示消息“应用程序已意外停止,请重试”我已经引用了堆栈溢出中的几乎所有查询,但无法获得任何可能的结果回答我的场景.. 我尝试删除应用程序中的 GPS 和 WIFI 文件,并用 TextView 替换切换按钮,然后它工作正常。
我包括我的代码,请验证它并帮助我的朋友。
Main.java
<块引用>打包project.sow;
导入 android.app.TabActivity;
导入 android.content.Intent;
导入 android.content.res.Resources;
导入 android.os.Bundle;
导入 android.widget.TabHost;
公共类 Main 扩展 TabActivity {
私有 TabHost tabHost;
@覆盖
公共无效onCreate(捆绑保存实例状态){
尝试{
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("开始").setIndicator("开始", 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 = 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;
导入 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;
公共类 Start 扩展 Activity {
公共 WifiManager wifi;
布尔状态;
上下文上下文;
公共 TextView 文本状态;
公共位置管理器位置管理器;
公共位置监听器位置监听器;
公共广播接收器接收器;
公共 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;
导入 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 {
私有静态最终字符串TAG =“WiFiFilter”;
开始开始;
公共 WiFiFilter(开始开始) {
超级();
this.start=开始;
}
//准备隐藏SSID并保存wifi配置供以后使用
@覆盖
公共无效onReceive(上下文c,意图意图){
列出结果 = start.wifi.getScanResults();
列表结果2 = null;
扫描结果过滤器 = null;
//过滤SSID
for(扫描结果过滤结果:结果){
if(filterresult.SSID=="VOLSBB")
{
结果2 = 结果;}
}
//检查信号电平
for (扫描结果结果:结果2) {
if (过滤器== null
|| WifiManager.compareSignalLevel(filter.level, result.level) < 0)
过滤器=结果;
尝试{
WifiManager.ACTION_PICK_WIFI_NETWORK.equals(过滤器);
}
catch(Exception e){String res="异常:无网络";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);
}
} }
gpsLocation.java
打包project.sow;
导入project.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;
双 b;
@SuppressWarnings("未使用")
私有字符串 bestProvider;
//公共LocationManager locationManager;
公共字符串提供商1;
公共 gpsLocation(开始 sowWifigps) {
超级();
this.sowWifigps=sowWifigps;
// TODO 自动生成的构造函数存根
Criteria 标准 = new Criteria();
//bestProvider = sowWifigps.locationManager.getBestProvider(criteria, false);
尝试{
if(LocationManager.GPS_PROVIDER==null){
提供商1 = LocationManager.NETWORK_PROVIDER;
}
否则{provider1=LocationManager.GPS_PROVIDER;}
}catch( 异常 e){provider1=sowWifigps.locationManager.getBestProvider(criteria, false);}
}
@覆盖
公共无效onLocationChanged(位置位置){
// TODO 自动生成的方法存根
sowWifigps.locationManager.requestLocationUpdates(provider1, 12000, 200, sowWifigps.locationListener);
}
@覆盖
公共无效onProviderDisabled(字符串提供者){
// 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); //更改站点名称
}
@覆盖
公共无效onProviderEnabled(字符串提供者){
// 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); //更改站点名称
}
@覆盖
public void onStatusChanged(Stringprovider,intstatus,Bundleextras){
// TODO 自动生成的方法存根
sowWifigps.locationListener.onLocationChanged(location);
}
}
我在这里展示第二个选项卡,其他选项卡与此类似。因此我省略它。
Info.java
打包project.sow;
导入 android.app.Activity;
导入 android.os.Bundle;
公共类信息扩展活动{
公共无效onCreate(捆绑保存实例状态){
super.onCreate(savedInstanceState);
}
@覆盖
protected void onResume() {
CustomWebView webView = new CustomWebView(this);
webView.loadUrl("http://www.google.com/?s=Info ”);
setContentView(webView);
super.onResume();
} }
<块引用>CustomWebView.java
包项目.sow;
导入 android.app.ProgressDialog;
导入 android.content.Context;
导入 android.graphics.Bitmap;
导入 android.view.View;
导入 android.webkit.WebView;
导入 android.webkit.WebViewClient;
公共类 CustomWebView 扩展 WebView {
私有 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 {
@覆盖
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (!CustomWebView.this.pgBar.isShowing()) {
CustomWebView.this.pgBar.show();
}
}
@覆盖
public void onPageFinished(WebView视图,字符串url){
super.onPageFinished(视图, url);
if (CustomWebView.this.pgBar.isShowing()) {
CustomWebView.this.pgBar.dismiss();
}
}
@覆盖
公共布尔shouldOverrideUrlLoading(WebView视图,字符串url){
尝试{
view.loadUrl(url);
}catch(异常){
}
返回真;
} } }
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>
我在清单文件中添加了以下权限
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" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WI-FI 和 GPS 将在主线程中调用,而不是在子线程中调用。
所以在main.java中,
要放置的是以前在start.java中调用的初始化
WI-FI and GPS are to be called in the main threads rather than in child threads..
So in the main.java,
Are to be placed which are formerly called initialized in the start.java