Ascending understanding of observer patterns in Android
Last Update:2016-07-04
Source: Internet
Author: User
<span id="Label3"></p><p><p>Prior to the Observer pattern in Java only a little fur, in the process of contact with android, gradually realized that the observer pattern is so important, many parts of Android has used the observer mode such as Contentresolver operation, To summarize the use of observer patterns in Android</p></p><p><p>The Observer pattern I understand is this:</p></p><p><p>As an example: there is an int type of data i in the service, I start a timer in the service to keep updating this value, when I start the service in the activity once, Whenever I change, my activity is automatically synchronized to update this value to show</p></p><p><p>1: Observer Body >> interface</p></p><p><p>The purpose of defining this interface is to deal with changes in the data that are placed in our parameters when the data changes.</p></p><p><p>In general, when we define this interface, we use the data we are interested in as a parameter in the method "for example, If we care about a location, then the parameter is either a location type or a total number in a database. the parameter is an int type"</p></p><p><p>2: Observer operator >> interface object</p></p><pre><pre> <span style="color: #008000;">//</span> <span style="color: #008000;">Service in</span> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">Static</span> <span style="color: #000000;">infochangeobserver observer; </span> <span style="color: #008000;">//</span> <span style="color: #008000;">Non-service</span> <span style="color: #0000ff;"></span> public <span style="color: #000000;">infochangeobserver observer; </span> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">void</span> <span style="color: #000000;">setobserver (infochangeobserver o) { </span>this. observer =<span style="color: #000000;"> o; }</span> <span style="color: #0000ff;"></span></pre></pre><p><p>3: Observer user >> Assigning a value to an interface object</p></p><p><p>Since we cannot directly new a service object in the service, we generally define the interface object as a static type and assign the value directly in activity through Myservice.observer = this [ The premise is that your activity implements the observer principal interface]; in addition, we will assign values through the Setcurrentobserver (infochangeobserver Obsler) provided by the Observer Operator.</p></p><p><p>Next time Source:</p></p><p><p>Interface interface:</p></p><pre><pre>package <span style="color: #000000;">com.example.performclickk;</span> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">Interface</span> <span style="color: #000000;">infochangeobserver{ </span>public<span style="color: #0000ff;"></span><span style="color: #0000ff;">int</span> ondatachanged (<span style="color: #0000ff;">int</span> <span style="color: #000000;"> i);}</span></pre></pre><p><p>Service</p></p><pre><span style="color: #000000;">package <span style="color: #000000;">com.example.performclickk;import java.util.timer;import Java.util.timertask;import android.app.Service ; Import Android.content.intent;import Android.os.ibinder;import android.util.Log;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">MyService extends service{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Listener Object</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span><span style="color: #000000;"><span style="color: #000000;">Infochangeobserver observer; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate () {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span>LOG.I (<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">ser</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">MyService onCreate</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;"> ); Super.oncreate (); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">variables to monitor</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i =<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">; Timer timer; TimerTask task; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>OnStart (Intent Intent,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Startid) {timer</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Timer (); Task</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">timertask () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Run () {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Analog Long Connection operation</span></span>Thread.Sleep (<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;"> ); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(interruptedexception E) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Catch block</span></span><span style="color: #000000;"><span style="color: #000000;">E.printstacktrace (); } I</span></span>++<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">listener <span style="color: #008000;">listens for data changes and puts the changed data into ondatachanged (i)</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(observer! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {observer.ondatachanged (i); } } }; Timer.schedule (task,</span></span><span style="color: #800080;"><span style="color: #800080;">0</span></span>,<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;"> ); Super.onstart (intent, startid); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">IBinder onbind (Intent Intent) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; }}</span></span></pre><p><p>3: User Activity</p></p><pre><span style="color: #000000;">package <span style="color: #000000;">Com.example.performclickk;import Java.io.file;import Java.io.ioexception;import Java.io.unsupportedencodingexception;import Java.util.hashmap;import Java.util.map;import java.util.zip.Deflater; Import Org.apache.http.httpentity;import Org.apache.http.httpresponse;import Org.apache.http.httpstatus;import Org.apache.http.httpversion;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httppost;import Org.apache.http.entity.mime.multipartentity;import Org.apache.http.entity.mime.content.contentbody;import Org.apache.http.entity.mime.content.filebody;import Org.apache.http.entity.mime.content.stringbody;import Org.apache.http.impl.client.defaulthttpclient;import ORG.APACHE.HTTP.</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">params</span></span><span style="color: #000000;"><span style="color: #000000;">. Coreprotocolpnames;import Org.apache.http.util.entityutils;import Android.os.bundle;import android.os.Environment ; Import Android.os.handler.callback;import Android.os.ibinder;import Android.app.activity;import Android.content.componentname;import Android.content.context;import Android.content.dialoginterface;import Android.content.dialoginterface.ondismisslistener;import Android.content.intent;import Android.content.serviceconnection;import Android.content.dialoginterface.onclicklistener;import Android.util.Log ; Import android.view.menu;import android.view.view;import android.widget.button;import android.widget.Toast;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Mainactivity extends Activity implements android.view.View.OnClickListener, Infochangeobserver{Button button1, bu tton2; Context context; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Ceshi</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i =<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Context</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">; Myservice.observer</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">; Button1</span></span>=<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.button1); Button2</span></span>=<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.button2); Button1.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;"> ); Button2.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;"> ); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(v.getid ()) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.button1:</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">data is constantly changing</span></span>LOG.I (<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Tag</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, i+<span style="color: #800000;"><span style="color: #800000;">""</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.button2:</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Start the service</span></span>Intent Intent =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Intent (mainactivity.<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>, Myservice.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;"> ); Intent.setaction (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Com.example.performclickk.service</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;"> ); StartService (intent); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">: </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; } } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Monitor the latest data</span></span><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Ondatachanged (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">I) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. i =<span style="color: #000000;"><span style="color: #000000;">i; }}</span></span></pre><p><p></p></p><p><p>Ascending understanding of observer patterns in Android</p></p></span>