First Android App: Simple SpeedHud (Hilfe)

  • Antworten:2
  • Bentwortet
Danny Hess
  • Forum-Beiträge: 17

29.11.2012, 02:51:08 via Website

Servus,

ich bin neu hier im Forum und ein noch unerfahrener Android Programierer und benötige eure hilfe. Ich versuche mich gerade an einer App die einem die Geschwindigkeit mit hilfe des GPS anzeigt.

Ich habe die App, dank probieren, schon soweit fertig, aber einige Funktionen funktionieren nicht so wie ich will.

Ich möchte gerne in der MainActivity einen Button der die App sofort beendet und nicht mehr im hintergrund läuft:

Hier der Code ohne diesen Button:
1import java.util.List;
2
3import android.app.Activity;
4import android.content.Context;
5import android.content.Intent;
6import android.location.Criteria;
7import android.location.Location;
8import android.location.LocationListener;
9import android.location.LocationManager;
10import android.location.LocationProvider;
11import android.os.Bundle;
12import android.os.PowerManager;
13import android.provider.Settings;
14import android.util.Log;
15import android.view.Menu;
16import android.view.View;
17import android.widget.TextView;
18
19public class MainActivity extends Activity {
20
21 private static final String TAG = MainActivity.class.getSimpleName();
22 private TextView speed;
23 private LocationManager manager;
24 private LocationListener listener;
25 protected PowerManager.WakeLock mWakeLock;
26
27 @Override
28 public void onCreate(Bundle savedInstanceState) {
29 super.onCreate(savedInstanceState);
30 setContentView(R.layout.activity_main);
31
32 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
33 this.mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
34 this.mWakeLock.acquire();
35
36 speed = (TextView) findViewById(R.id.textView1);
37
38 manager = (LocationManager) getSystemService(LOCATION_SERVICE);
39
40 List<String> providers = manager.getAllProviders();
41 for (String name : providers) {
42 LocationProvider lp = manager.getProvider(name);
43 Log.d(TAG,
44 lp.getName() + " --- isProviderEnabled(): "
45 + manager.isProviderEnabled(name));
46 Log.d(TAG, "requiresCell(): " + lp.requiresCell());
47 Log.d(TAG, "requiresNetwork(): " + lp.requiresNetwork());
48 Log.d(TAG, "requiresSatellite(): " + lp.requiresSatellite());
49
50 }
51 Criteria criteria = new Criteria();
52 criteria.setAccuracy(Criteria.ACCURACY_FINE);
53 criteria.setPowerRequirement(Criteria.POWER_HIGH);
54 String name = manager.getBestProvider(criteria, true);
55 Log.d(TAG, name);
56 listener = new LocationListener() {
57
58 public void onLocationChanged(Location location) {
59 // TODO Auto-generated method stub
60 Log.d(TAG, "onLocationChanged()");
61 if (location != null) {
62 speed.setText(String.format("%2.0f",
63 (location.getSpeed() * 3.6)));
64 if (location.getSpeed() <= 0) {
65 speed.setText("000");
66 }
67 }
68 }
69
70 public void onProviderDisabled(String provider) {
71 // TODO Auto-generated method stub
72 Log.d(TAG, "onProviderDisabled()");
73 if (LocationManager.GPS_PROVIDER != null) {
74 speed.setText("GPS OFF");
75 speed.setTextSize(100);
76 }
77 }
78
79 public void onProviderEnabled(String provider) {
80 // TODO Auto-generated method stub
81 Log.d(TAG, "onProviderEnabled()");
82 }
83
84 public void onStatusChanged(String provider, int status,
85 Bundle extras) {
86 // TODO Auto-generated method stub
87 Log.d(TAG, "onStatusChanged()");
88 }
89
90 };
91
92 }
93
94 @Override
95 public boolean onCreateOptionsMenu(Menu menu) {
96 getMenuInflater().inflate(R.menu.activity_main, menu);
97 return true;
98 }
99
100 @Override
101 protected void onStart() {
102 super.onStart();
103 Log.d(TAG, "onStart()");
104 manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
105 listener);
106 }
107
108 @Override
109 protected void onPause() {
110 super.onPause();
111 Log.d(TAG, "onPause()");
112 manager.removeUpdates(listener);
113 }
114
115 public void hud(View view) {
116 Intent intent = new Intent(this, HudActivity.class);
117 startActivity(intent);
118 }
119
120 public void gps(View view) {
121 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
122 startActivity(intent);
123 }
124
125 @Override
126 protected void onDestroy() {
127 // TODO Auto-generated method stub
128 this.mWakeLock.release();
129 super.onDestroy();
130 }
131}

Darüber hinaus will ich einen Button in der HudActivity, der zur MainActivity zurückkehrt und die HudActivity schließt. Zur Zeit geht die Activity zurück, aber wenn ich den Back Button auf meinem Telefon drücke erscheint wieder HudActivity.

Hier der Code:
1import java.util.List;
2
3import android.location.Criteria;
4import android.location.Location;
5import android.location.LocationListener;
6import android.location.LocationManager;
7import android.location.LocationProvider;
8import android.os.Bundle;
9import android.os.PowerManager;
10import android.app.Activity;
11import android.content.Context;
12import android.content.Intent;
13import android.util.Log;
14import android.view.Menu;
15import android.view.View;
16import android.widget.TextView;
17
18public class HudActivity extends Activity {
19
20 private static final String TAG = MainActivity.class.getSimpleName();
21 private TextView hudSpeed;
22 private LocationManager manager;
23 private LocationListener listener;
24 protected PowerManager.WakeLock mWakeLock;
25
26 @Override
27 public void onCreate(Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState);
29 setContentView(R.layout.activity_hud);
30
31 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
32 this.mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
33 this.mWakeLock.acquire();
34
35 hudSpeed = (TextView) findViewById(R.id.textView1);
36
37 manager = (LocationManager) getSystemService(LOCATION_SERVICE);
38
39 List<String> providers = manager.getAllProviders();
40 for (String name : providers) {
41 LocationProvider lp = manager.getProvider(name);
42 Log.d(TAG,
43 lp.getName() + " --- isProviderEnabled(): "
44 + manager.isProviderEnabled(name));
45 Log.d(TAG, "requiresCell(): " + lp.requiresCell());
46 Log.d(TAG, "requiresNetwork(): " + lp.requiresNetwork());
47 Log.d(TAG, "requiresSatellite(): " + lp.requiresSatellite());
48
49 }
50 Criteria criteria = new Criteria();
51 criteria.setAccuracy(Criteria.ACCURACY_FINE);
52 criteria.setPowerRequirement(Criteria.POWER_HIGH);
53 String name = manager.getBestProvider(criteria, true);
54 Log.d(TAG, name);
55 listener = new LocationListener() {
56
57 public void onLocationChanged(Location location) {
58 // TODO Auto-generated method stub
59 Log.d(TAG, "onLocationChanged()");
60 if (location != null) {
61 hudSpeed.setText(String.format("%2.0f",
62 (location.getSpeed() * 3.6)));
63 if (location.getSpeed() <= 0) {
64 hudSpeed.setText("000");
65 }
66 }
67 }
68
69 public void onProviderDisabled(String provider) {
70 // TODO Auto-generated method stub
71 Log.d(TAG, "onProviderDisabled()");
72 if (LocationManager.GPS_PROVIDER != null) {
73 hudSpeed.setText("---");
74 }
75 }
76
77 public void onProviderEnabled(String provider) {
78 // TODO Auto-generated method stub
79 Log.d(TAG, "onProviderEnabled()");
80 }
81
82 public void onStatusChanged(String provider, int status,
83 Bundle extras) {
84 // TODO Auto-generated method stub
85 Log.d(TAG, "onStatusChanged()");
86 }
87
88 };
89 }
90
91 @Override
92 public boolean onCreateOptionsMenu(Menu menu) {
93 getMenuInflater().inflate(R.menu.activity_hud, menu);
94 return true;
95 }
96
97 @Override
98 protected void onStart() {
99 super.onStart();
100 Log.d(TAG, "onStart()");
101 manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
102 listener);
103 }
104
105 @Override
106 protected void onPause() {
107 super.onPause();
108 Log.d(TAG, "onPause()");
109 manager.removeUpdates(listener);
110 }
111
112 public void back(View view) {
113 Intent intent = new Intent(this, MainActivity.class);
114 startActivity(intent);
115 }
116
117 @Override
118 protected void onDestroy() {
119 // TODO Auto-generated method stub
120 this.mWakeLock.release();
121 super.onDestroy();
122 }
123}


Währe genial wenn ich von euch hilfe bekommen würde.

grüße Danny



PS: Wenn einer eine Idee hat wie ich einen Linearen Kopass erstellen kann sagt mir bitte bescheid.

— geändert am 30.11.2012, 23:34:24

Antworten
Gelöschter Account
  • Forum-Beiträge: 281

29.11.2012, 12:15:39 via Website

Also - die führenden Köpfe der Android-Entwicklung sind ja der Meinung, dass Exit-Buttons generell eine schlechte Idee sind. Prüfe lieber mal, warum deine App "weiterläuft", wenn Sie über den Home-Button oder ausreichend häufiges Drücken der Back-Taste am Phone verlassen wird. Stichwort: Nicht passierender Entfernung des Listeners aus der Benachrichtigungsliste des LocationManagers.

Antworten
Danny Hess
  • Forum-Beiträge: 17

30.11.2012, 23:32:28 via Website

Danke für die Antwort, habe den Quellcode noch mal überprüft und angepasst und alles läuft wie es soll :D

Antworten