How does Android service disable activity?

Source: Internet
Author: User

Myserviceactivity instance ){
Myactivity = instance;
}

Public myserviceactivity getinstance (){
Return myactivity;
}
}
2. Save the instance in Activity
Public class myserviceactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
(MyApp) getapplication (). setinstance (this );
......
}
}
3. Retrieve the instance from the service
Public class myservice extends Service {
Myserviceactivity myactivity;
@ Override
Public void oncreate (){
Super. oncreate ();
Setforeground (true );
Android. OS. Debug. waitfordebugger ();
Myactivity = (MyApp) getapplication (). getinstance ();
......
}
}

I tried the first method, which is feasible. However, when aidl is encountered, I am afraid I cannot use these two methods to close the activity started by the Service. I have tried many methods that fail. For example, if I assign an activity value to a static variable, no matter how the value is passed, the variable is null in the method called by aidl. After reading the relevant information, I understand that aidl does not support static parameters, that is, calling any static parameters in aidl is null. This is just my understanding, and there may be errors! Hope the experts can correct them in time! At that time, my requirement was as follows: a client needs to continuously send requests to open the page through my aidl, so I need to enable a thread in the service, when a new request is sent, I open the page. The previously written program either simply opens a page and remains unchanged, or starts a new activity without stopping, and then presses the back key to remove many of the activities, after all the activities are closed, the new activity opened by the service does not change. Therefore, when I need the service to open a new page, finish the previous activity. After trying many methods, I can only use broadcast. I use service to send a broadcast, and then the activity will be closed after receiving the broadcast. However, the broadcast class inherits broadcastreceive. How can this class disable the activity? Therefore, you can only write the broadcast in acivity. The Code is as follows: public class anhttptool extends activity {private webview mwebview; private context; private string mtrafficid; private string murl; private string mapn; private long mras2ftp; private long mintervaltime; private int mtimes; private anhttptool AHT;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {// start mservice httpmanagerservice. start Service (anhttptool. this); super. oncreate (savedinstancestate); setcontentview (R. layout. browser); AHT = This; mtrafficid = getintent (). getstringextra ("mtrafficid"); murl = getintent (). getstringextra ("murl"); mapn = getintent (). getstringextra ("mapn"); mras2ftp = getintent (). getlongextra ("mras2ftp", 0); mintervaltime = getintent (). getlongextra ("mintervaltime", 0); mtimes = getintent (). getintextra ("mtimes ", 1); mwebview = (webview) findviewbyid (R. id. webview01); mwebview. loadurl (murl); websettings mwebsettings = mwebview. getsettings (); mywebviewclient mwebviewclient = new mywebviewclient (); mwebview. setwebviewclient (mwebviewclient); // adds an intent filter, and adds the new intentfilter (); filter that the consumer er listens to the broadcast intentfilter sent by the Service. addaction ("broadcast one! "); Registerreceiver (mreceiver, filter);} protected broadcastreceiver mreceiver = new broadcastreceiver () {@ overridepublic void onreceive (context, intent) {// todo auto-generated method stubstring action = intent. getaction (); If (action. equals ("broadcast one! ") {AHT. finish (); // your code here }};} class mywebviewclient extends webviewclient {// The webpage starts loading @ overridepublic void onpagestarted (webview view, string URL, bitmap favicon) {// todo auto-generated method stublog. D ("H3C", "pagestarted"); super. onpagestarted (view, URL, favicon);} // end loading @ overridepublic void onpagefinished (webview view, string URL) {// todo auto-generated method stublog. D ("H3C", "pagefinished"); super. onpagefinished (view, URL);} // open a new webpage in webview @ overridepublic Boolean shouldoverrideurlloading (webview view, string URL) {// todo auto-generated method stubview. loadurl (URL); Return true ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.