Android determines if the current activity is the last activity and whether the app or activity exists
Last Update:2015-06-04
Source: Internet
Author: User
<span id="Label3"></p>Judging the current activity is the last activity:<p><p></p></p><p><p>In the activity method, there is a method Istaskroot () method, This method can determine whether the current activity is the last activity, if it is, the user then press the return key to exit the application, or return to the previous activity is still active;</p></p><p><p>The code is as Follows:</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">@Overridepublic void onbackpressed () {if (istaskroot ()) {toast.maketext (mcontext, "the Current activity is the only, a surviving activity" of the app, 0 ). Show ();} else {toast.maketext (mcontext, "the Current activity is not the only active activity of the app," 0). show (); Super.onbackpressed ();}</pre></pre><br><p><p></p></p><strong>to <strong>determine whether an application or activity exists:</strong></strong><p><p></p></p><p><p></p></p><span style="font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial; font-size:14px; line-height:26px">determine whether an application exists based on the package name</span></span><span style="font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:Arial; font-size:14px; line-height:26px"></span></span><p><p></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">public boolean checkapplication (<em>String</em> Packagename) {</p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">if (packagename = = NULL | | "". equals (packagename)) {</p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">Return false;</p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">}</p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">try {</p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">ApplicationInfo info = Getpackagemanager (). getapplicationinfo (packagename, packagemanager.get_uninstalled_packages );<br>Return true;<br>} catch (namenotfoundexception E) {<br>Return false;<br>}</p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">}</p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><br></p></p>second, determine whether the activity exists<p><p></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">Intent Intent = new Intent ();<br>Intent.setclassname ("package name", "class name");</p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"></p></p>Method one: if (getpackagemanager (). resolveactivity (intent, 0) = = Null) {<p><p></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">Indicates that the activity does not exist in the system<br>}</p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"></p></p>Method two: if (intent.resolveactivity (getpackagemanager ()) = = Null) {<p><p></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">Indicates that the activity does not exist in the system<br>}<br></p></p>Method three: list<resolveinfo> List = Getpackagemanager (). queryintentactivities (intent, 0);<br>If (list.size () = = 0) {<br>Indicates that the activity does not exist in the system<br>}<p><p></p></p><br><p><p><br></p></p><p><p>Android determines if the current activity is the last activity and whether the app or activity exists</p></p></span>