0

こんにちは、私は最適な場所を特定し、それを番号に SMS で送信するアプリを開発しています。ネットワークの場所で問題なく動作しますが、GPSまたは基準クラスで場所を特定したい場合、強制的に閉じられます!! 私を助けてくれてとても親切です。

package ir.M410.toolkit;

import android.app.Service;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.util.Log;



public class LocationFinder extends Service implements LocationListener{

double lat ,lon ;
@Override
public IBinder onBind(Intent arg0) {
    return null;
       }

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.


    LocationManager mlocationManager = (LocationManager)        getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
    String locationprovider = mlocationManager.getBestProvider(criteria, true);
    Location mlocation = mlocationManager.getLastKnownLocation(locationprovider);


      lat = mlocation.getLatitude ();
      lon = mlocation.getLongitude ();

      Log.i("Geo_Location", "Latitude: " + lat + ", Longitude: " + lon);

      SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage("+11231233213", null,"https://maps.google.com/maps?q="+lat+","+lon, null, null);



        //  SmsManager sms = SmsManager.getDefault();
    //sms.sendTextMessage(MainActivity.senderNum, null,"  "+"lat:"+lat+"  "+"lon:"+lon, null, null);
 //stopSelf();
          return START_NOT_STICKY;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

     lat= location.getLatitude();
    lon =location.getLongitude();

    Log.i("Geo_Location", "Latitude: " + lat + ", Longitude: " + lon);
  //  stopSelf();
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}


@Override
public void onDestroy() {
    super.onDestroy();

}
}

これは私のマニフェストです:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ir.M410.toolkit"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.SEND_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name="ir.M410.toolkit.Broadcast_Receiver" >
        <intent-filter android:priority="2147483647" >
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="ir.M410.toolkit.android.action.broadcast" />
        </intent-filter>
    </receiver>

    <service android:name="ir.M410.toolkit.LocationFinder" />

    <activity
        android:name="ir.M410.toolkit.PasswordCheck"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="ir.M410.toolkit.MainActivity"
        android:label="@string/title_activity_main"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="ir.M410.toolkit.Teturial"
        android:label="@string/title_activity_teturial" >
    </activity>
    <activity
        android:name="ir.M410.toolkit.CallDivertActivity"
        android:label="@string/title_activity_call_divert" >
    </activity>
</application>

</manifest>

[編集済み] そしてここに LogCat の定義があります:

10-14 19:58:37.823: E/AndroidRuntime(2685): FATAL EXCEPTION: main
10-14 19:58:37.823: E/AndroidRuntime(2685): java.lang.RuntimeException: Unable to start service ir.M410.toolkit.LocationFinder@4482e4a8 with Intent { cmp=ir.M410.toolkit/.LocationFinder }: java.lang.NullPointerException
10-14 19:58:37.823: E/AndroidRuntime(2685):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3260)
10-14 19:58:37.823: E/AndroidRuntime(2685):     at android.app.ActivityThread.access$3600(ActivityThread.java:135)
10-14 19:58:37.823: E/AndroidRuntime(2685):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2205)
10-14 19:58:37.823: E/AndroidRuntime(2685):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 19:58:37.823: E/AndroidRuntime(2685):     at android.os.Looper.loop(Looper.java:143)
10-14 19:58:37.823: E/AndroidRuntime(2685):     at android.app.ActivityThread.main(ActivityThread.java:4914)
10-14 19:58:37.823: E/AndroidRuntime(2685):     at java.lang.reflect.Method.invokeNative(Native Method)
10-14 19:58:37.823: E/AndroidRuntime(2685):     at java.lang.reflect.Method.invoke(Method.java:521)
10-14 19:58:37.823: E/AndroidRuntime(2685):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-14 19:58:37.823: E/AndroidRuntime(2685):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-14 19:58:37.823: E/AndroidRuntime(2685):     at dalvik.system.NativeStart.main(Native Method)
10-14 19:58:37.823: E/AndroidRuntime(2685): Caused by: java.lang.NullPointerException
10-14 19:58:37.823: E/AndroidRuntime(2685):     at ir.M410.toolkit.LocationFinder.onStartCommand(LocationFinder.java:38)
10-14 19:58:37.823: E/AndroidRuntime(2685):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3246)
10-14 19:58:37.823: E/AndroidRuntime(2685):     ... 10 more

[編集済み] null でないことを確認するコード:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.


    LocationManager mlocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
    String locationprovider = mlocationManager.getBestProvider(criteria, true);
    if(mlocationManager.getLastKnownLocation(locationprovider)!=null){
    Location mlocation = mlocationManager.getLastKnownLocation(locationprovider);


      lat = mlocation.getLatitude ();
      lon = mlocation.getLongitude ();

      Log.i("Geo_Location", "Latitude: " + lat + ", Longitude: " + lon);

      SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage("+11231233213", null,"https://maps.google.com/maps?q="+lat+","+lon, null, null);

    }

そして、大丈夫で、強制的に閉じることはできませんが、データが受信されません。常にnullです!!

4

1 に答える 1