我剛開始我的第一個主要項目 - 一個簡單的防盜應用程序,當我的手機被文本編輯時,如果它包含一個特定的字符串,它會發送一個短信到原始地址,告訴它手機的位置。但是,我現在只是試圖讓手機收到短信,檢查它是否包含字符串,獲取位置,然後在手機上烘烤位置。我的問題是,因爲我對android編程/ Java非常非常陌生(僅15)我將我的位置從MainActivity傳遞到Broadcast Receiver時遇到問題。我得到一個空指針異常 - 這是當我的應用程序設法獲取gps位置時。另外需要注意的是,我使用'Cerberus防盜'作爲我的主要防盜功能,可能會導致任何錯誤?空指針異常 - 無法啓動接收器?
這是我的MainActivity - 在我的LocationListener中,正如你所看到的,我已經注意到onLocationChanged上的Toast.makeText(),如果我取消註釋,我會得到正確的座標。這意味着它必須與我的意圖的東西有關 - 這對我來說看起來完全沒問題。
package com.SMS;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class MainActivity extends Activity {
private LocationManager lm;
private LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
if (loc != null)
{
int Longitude = (int) (loc.getLongitude() * 1E6);
int Latitude = (int) (loc.getLatitude() * 1E6);
// Toast.makeText(getBaseContext(), "Location Changed : Lat: " + Latitude + " Longitude : " + Longitude, Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, SMSReceiver.class); intent.setAction("android.provider.Telephony.SMS_RECEIVED");
intent.putExtra("Longitude", Longitude);
intent.putExtra("Latitude", Latitude);
sendBroadcast(intent);
}
}
@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
}
}
}
這是我的SmsReceiver類,這是檢索sms以檢查包含字符串的位置。我嘗試了我在這類事情上看到的一切(我的歷史充滿了這些東西),但是我做了什麼,經度和緯度仍然返回300--我設置的默認值。我確信這是與我的意圖設置和接收意圖有關,只是不知道什麼是錯的。因此,請仔細閱讀並向我反饋任何您發現錯誤的內容。
package com.SMS;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//--get the sms message passed in--
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//--- retrieve the sms message received
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " : ";
str += msgs[i ].getMessageBody().toString();
str += "\n";
if(str.contains("alfa"))
{
int Longitude = bundle.getInt("Longitude", 300);
int Latitude = bundle.getInt("Latitude", 300);
//--display the new sms message
Toast.makeText(context, "Location Changed : Latitude : " + Latitude + " Longitude : " + Longitude, Toast.LENGTH_LONG).show();
}
}
}
}
}
這裏是我的清單,對此我相當肯定是問題的一部分,如果你看到任何錯誤或出位,或者可以簡化,請告訴我!
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.SMS"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.SMS.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>
<receiver android:name=".SMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
最後,LogCat錯誤是我找到gps時得到的。
04-01 15:38:41.323: W/dalvikvm(16245): threadid=1: thread exiting with uncaught exception (group=0x412b7930)
04-01 15:38:41.333: E/AndroidRuntime(16245): FATAL EXCEPTION: main
04-01 15:38:41.333: E/AndroidRuntime(16245): java.lang.RuntimeException: Unable to start receiver com.SMS.SMSReceiver: java.lang.NullPointerException
04-01 15:38:41.333: E/AndroidRuntime(16245): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2383)
04-01 15:38:41.333: E/AndroidRuntime(16245): at android.app.ActivityThread.access$1500(ActivityThread.java:141)
04-01 15:38:41.333: E/AndroidRuntime(16245): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
04-01 15:38:41.333: E/AndroidRuntime(16245): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 15:38:41.333: E/AndroidRuntime(16245): at android.os.Looper.loop(Looper.java:137)
04-01 15:38:41.333: E/AndroidRuntime(16245): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-01 15:38:41.333: E/AndroidRuntime(16245): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 15:38:41.333: E/AndroidRuntime(16245): at java.lang.reflect.Method.invoke(Method.java:511)
04-01 15:38:41.333: E/AndroidRuntime(16245): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-01 15:38:41.333: E/AndroidRuntime(16245): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-01 15:38:41.333: E/AndroidRuntime(16245): at dalvik.system.NativeStart.main(Native Method)
04-01 15:38:41.333: E/AndroidRuntime(16245): Caused by: java.lang.NullPointerException
04-01 15:38:41.333: E/AndroidRuntime(16245): at com.SMS.SMSReceiver.onReceive(SMSReceiver.java:24)
04-01 15:38:41.333: E/AndroidRuntime(16245): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2376)
04-01 15:38:41.333: E/AndroidRuntime(16245): ... 10 more
請,請,請幫助我,我一直在尋找,現在5天了互聯網,但仍沒有運氣。我似乎無法得到它的工作。因此,如果您可以告訴我哪些問題以及如何更改,或者對此有任何疑問,請回復,因爲我花了幾個小時在網上搜索。此外,如果您發現任何「錯誤的編碼」,請不要猶豫,告訴我。非常感謝你,如果你能解決這個問題!
對'bundle.get(「pdus」)'的調用返回'null'。你有沒有用「'pdus」'鍵來放入包中? – armandino