2012-07-08 299 views
0

我通過短信發送座標的應用程序有問題。 當我添加電池電量時,捕捉修復程序後應用程序停止工作。 (座標不在顯示,但他們是通過短信。)Android電池電量

請幫助。

ZoltrixGPSActivity

package com.zoltrix.gps; 

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class ZoltrixGPSActivity extends Activity { 

    // Here I added a level of battery (1) 

    private TextView contentTxt; 
    private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context arg0, Intent intent) { 
      // TODO Auto-generated method stub 
      int level = intent.getIntExtra("level", 0); 
      contentTxt.setText(String.valueOf(level) + "%"); 
     } 
    }; 

    // end 

    TextView textLat; 
    TextView textLong; 
    TextView textAlt; 
    TextView textPro; 
    TextView textAcc; 
    TextView textSpeed; 
    public String onLocat; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Here I added a level of battery (2) 

     contentTxt = (TextView) this.findViewById(R.id.battery); 
     this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(
       Intent.ACTION_BATTERY_CHANGED)); 

     // end 

     Button btn1 = (Button) findViewById(R.id.buttonExit); 
     btn1.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // exit 
       finish(); 
       System.exit(0); 
      } 
     }); 

     textLat = (TextView) findViewById(R.id.textLat); 
     textLong = (TextView) findViewById(R.id.textLong); 
     textAlt = (TextView) findViewById(R.id.textAlt); 
     textPro = (TextView) findViewById(R.id.textPro); 
     textAcc = (TextView) findViewById(R.id.textAcc); 
     textSpeed = (TextView) findViewById(R.id.textSpeed); 

     LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     LocationListener ll = new mylocationlistener(); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 
    } 

    class mylocationlistener implements LocationListener { 

     public void onLocationChanged(Location location) { 
      if (location != null) { 
       double pLong = location.getLongitude(); 
       double pLat = location.getLatitude(); 
       double pAlt = location.getAltitude(); 
       String PPro = location.getProvider(); 
       float PAcc = location.getAccuracy(); 
       float PSpeed = location.getSpeed(); 

       textLat.setText(Double.toString(pLat)); 
       textLong.setText(Double.toString(pLong)); 
       textAlt.setText(Double.toString(pAlt)); 
       textPro.setText(PPro); 
       textAcc.setText(Float.toString(PAcc)); 
       textSpeed.setText(Double.toString(PSpeed)); 

       Intent i = new Intent(ZoltrixGPSActivity.this, 
         SendSMSActivity.class); 
       i.putExtra("lon", Double.toString(pLong)); 
       i.putExtra("lat", Double.toString(pLat)); 
       i.putExtra("alt", Double.toString(pAlt)); 
       i.putExtra("acc", Float.toString(PAcc)); 
       i.putExtra("spe", Float.toString(PSpeed)); 
       startActivity(i); 

      } 

     } 

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

     } 

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

     } 

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

     } 

    } 

} 

SendSMSActivity

package com.zoltrix.gps; 

import android.app.Activity; 
import android.os.Bundle; 
import android.telephony.SmsManager; 
import android.view.View; 
import android.widget.Button; 

public class SendSMSActivity extends Activity { 
    Button btnSendSMS; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     btnSendSMS = (Button) findViewById(R.id.btnSendSMS); 
     btnSendSMS.setOnClickListener(new View.OnClickListener() 

     { 

      public void onClick(View v) { 

       Bundle extras = getIntent().getExtras(); 

       String LON = extras.getString("lon"); 
       String LAT = extras.getString("lat"); 
       String ALT = extras.getString("alt"); 
       String ACC = extras.getString("acc"); 
       String SPE = extras.getString("spe"); 

       sendSMS("510104727", "LON" + LON + "#" + "LAT" + LAT + "#" 
         + "ALT" + ALT + "#" + "ACC" + ACC + "#" + "SPE" + SPE); 
      } 

     }); 
    } 

    // ---sends an SMS message to another device--- 
    private void sendSMS(String phoneNumber, String message) { 
     SmsManager sms = SmsManager.getDefault(); 
     sms.sendTextMessage(phoneNumber, null, message, null, null); 
    } 

} 

艙單

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

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

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
    <uses-permission android:name="android.permission.SEND_SMS" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name="com.zoltrix.gps.SendSMSActivity" 
      android:label="@string/app_name" > 
     </activity> 
     <activity 
      android:name="com.zoltrix.gps.ZoltrixGPSActivity" 
      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> 

Loyout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="top" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Latitude" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/textLat" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Longitude " 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/textLong" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Alt" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/textAlt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Provider" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/textPro" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Accuracy (m)" /> 

    <TextView 
     android:id="@+id/textAcc" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/text344" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Speed (m/s)" /> 

    <TextView 
     android:id="@+id/textSpeed" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/battery" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 




    <Button 
     android:id="@+id/btnSendSMS" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Send" /> 

    <Button 
     android:id="@+id/buttonExit" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Exit" /> 

</LinearLayout> 

logcat的 http://pastebin.com/whhdHsyw

回答

2

嘗試添加android.permission.BATTERY_STATS權限代碼。

<uses-permission android:name="android.permission.BATTERY_STATS" /> 
+0

謝謝,它幫助,但不顯示在屏幕上的座標。當手機在那裏修復一秒鐘並隨着電池電量的文本消失。 – Bart 2012-07-08 16:02:05

0
// Put this Code into your MainActivity where you want to show level 

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context c, Intent i) { 
     int level = i.getIntExtra("level", 0); 
     ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar); 
     pb.setProgress(level); 
     TextView tv = (TextView) findViewById(R.id.textfield); 
     tv.setText("Battery Level: " + Integer.toString(level) + "%"); 
    } 

}; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    registerReceiver(mBatInfoReceiver, new IntentFilter(
      Intent.ACTION_BATTERY_CHANGED)); 
} 
// Please Add the given below code into manifest: 

<uses-permission android:name="android.permission.BATTERY_STATS" />