android_learning:headfirst_android_development_notes:chapter_13
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
android_learning:headfirst_android_development_notes:chapter_13 [2016/04/30 02:57] – [p. 553] mithat | android_learning:headfirst_android_development_notes:chapter_13 [2016/05/07 06:04] (current) – [p. 553] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Chapter 13 ====== | ====== Chapter 13 ====== | ||
- | <WRAP center round info 60%> | + | <WRAP center round important |
- | Under development. | + | If you read nothing else here, you should check out the section on [[# |
</ | </ | ||
Line 55: | Line 55: | ||
===== p. 553 ===== | ===== p. 553 ===== | ||
- | In other words, while most Intent | + | In other words, while most IntentService |
===== p. 554 ===== | ===== p. 554 ===== | ||
Line 245: | Line 245: | ||
===== Permissions? | ===== Permissions? | ||
- | You will probably see some editor rage even after you add the needed | + | You will probably |
> If the device is running Android 5.1 or lower, **or** your app's target SDK is 22 or lower: If you list a dangerous permission in your manifest, the user has to grant the permission when they install the app; if they do not grant the permission, the system does not install the app at all. | > If the device is running Android 5.1 or lower, **or** your app's target SDK is 22 or lower: If you list a dangerous permission in your manifest, the user has to grant the permission when they install the app; if they do not grant the permission, the system does not install the app at all. | ||
Line 273: | Line 273: | ||
The IDE will tell you that the project needs to be synced, which you should do. And for good measure, from the menu bar, do a //Build > Clean Project//. | The IDE will tell you that the project needs to be synced, which you should do. And for good measure, from the menu bar, do a //Build > Clean Project//. | ||
+ | |||
+ | ===== Code block ===== | ||
+ | <file java MainActivity.java> | ||
+ | package com.hfad.odometer; | ||
+ | |||
+ | import android.app.Activity; | ||
+ | import android.content.ComponentName; | ||
+ | import android.content.Context; | ||
+ | import android.content.Intent; | ||
+ | import android.content.ServiceConnection; | ||
+ | import android.os.Bundle; | ||
+ | import android.os.Handler; | ||
+ | import android.os.IBinder; | ||
+ | import android.widget.TextView; | ||
+ | |||
+ | public class MainActivity extends Activity { | ||
+ | |||
+ | private OdometerService odometer; | ||
+ | private boolean bound = false; | ||
+ | |||
+ | private ServiceConnection connection = new ServiceConnection() { | ||
+ | @Override | ||
+ | public void onServiceConnected(ComponentName componentName, | ||
+ | OdometerService.OdometerBinder odometerBinder = | ||
+ | (OdometerService.OdometerBinder) binder; | ||
+ | odometer = odometerBinder.getOdometer(); | ||
+ | bound = true; | ||
+ | } | ||
+ | @Override | ||
+ | public void onServiceDisconnected(ComponentName componentName) { | ||
+ | bound = false; | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | @Override | ||
+ | protected void onCreate(Bundle savedInstanceState) { | ||
+ | super.onCreate(savedInstanceState); | ||
+ | setContentView(R.layout.activity_main); | ||
+ | watchMileage(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void onStart() { | ||
+ | super.onStart(); | ||
+ | Intent intent = new Intent(this, | ||
+ | bindService(intent, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void onStop() { | ||
+ | super.onStop(); | ||
+ | if (bound) { | ||
+ | unbindService(connection); | ||
+ | bound = false; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private void watchMileage() { | ||
+ | final TextView distanceView = (TextView)findViewById(R.id.distance); | ||
+ | final Handler handler = new Handler(); | ||
+ | handler.post(new Runnable() { | ||
+ | @Override | ||
+ | public void run() { | ||
+ | double distance = 0.0; | ||
+ | if (odometer != null) { | ||
+ | distance = odometer.getMiles(); | ||
+ | } | ||
+ | String distanceStr = String.format(" | ||
+ | distanceView.setText(distanceStr); | ||
+ | handler.postDelayed(this, | ||
+ | } | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
android_learning/headfirst_android_development_notes/chapter_13.1461985020.txt.gz · Last modified: 2016/04/30 02:57 by mithat