-2
我正在嘗試讀取我的GPS位置。它適用於仿真器Nexus S API 23 但是,將它安裝在4.1.1或5.0.1版本的Deices上時,它在工作之前會停止。我需要知道的是,如果代碼或因爲設備之間的不同版本的,因爲這個問題: 這是我的代碼:應用程序在仿真器上運行,但在手機上停止運行
Main Activity.java
package com.example.user.getgps1;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
static final long meter=1;
static final long mill=1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager manager=(LocationManager) this.getSystemService(context.LOCATION_SERVICE);
if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED||checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, meter, mill, new locationlist(this));
}
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,meter,mill,new locationlist(this));
}
ProgressDialog dialog;
Context context;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void bulocation(View view) {
context = this;
dialog = new ProgressDialog(MainActivity.this);
dialog.show();
dialog.setMessage("Getting Last Location");
LocationManager locationmanager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED||checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, meter, mill, new locationlist(this));
}
Location lastknown = locationmanager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(lastknown==null)
lastknown=locationmanager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
TextView t1=(TextView) findViewById(R.id.textView1);
TextView t2=(TextView) findViewById(R.id.textView2);
t2.setText(Double.toString(lastknown.getLatitude()));
t1.setText(Double.toString(lastknown.getLongitude()));
dialog.dismiss();
}
}
activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView1"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView2"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="bulocation"/>
</RelativeLayout>
Android_manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.getgps1" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
locationlist.java
package com.example.user.getgps1;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.Toast;
public class locationlist implements LocationListener {
Context context;
public locationlist(Context context)
{
this.context=context;
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(context,"log:"+Double.toString(location.getLongitude())+", lat: "+Double.toString(location.getLatitude()),Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(context,"GPS is Changed",Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(context,"GPS is on",Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(context,"GPS is OFF",Toast.LENGTH_LONG).show();
}
}
what is the problem
發佈崩潰的日誌輸出 –
請發佈完整的logcat輸出。 – Nanoc