Android Study Notes (7)-service and notification experience

Source: Internet
Author: User

I took a look at the service class in Android. app and thought it was very similar to activity, but I should pay attention to the following points:

1. In the lifecycle, it is easier to migrate services from oncreate ()-> onstart (INT, bundle)-> ondestroy. However, its onstart contains parameters. The first ID can be used to identify the service, and the second parameter display is used to transmit data. Compared with activity, the bundle that transmits data is included in oncreate.

2. Start of service starts from context. startservice. In fact, both activity and service are derived classes of context. End with context. stopservice () or its own stopself ().

3. Another difference between the service and the activity is that it can bind an existing service by another context. This is the method context. bindservice (). The Bound Service requires that it has been oncreate, but there can be no onstart. There is an abstract method getbinder () in the service class to get this ibinder object. For details about this, let's look at it later. Here we just make a record.

4. there is another security issue related to the service, which can be found in androidmanifest. in XML, the <uses-Permission> label is used to declare the access permission of a service. Please wait for the android security issue to be resolved.

I have always believed in a natural learning method, starting with the simplest things, won't feel boring.

The following is an example.

Modify the androidmanifest. xml file and add an activity and a service:

<Activity class = ". hellotwod" Android: Label = "hello_two_d">
</Activity>
<Service class = ". hellotwodservice"/>

The hellotwod. Java code is relatively simple, as follows:

Public class hellotwod extends activity implements onclicklistener
...{
Public hellotwod ()
...{
Super ();
}
Public void oncreate (bundle icicle )...{
Super. oncreate (icicle );
Settheme (Android. R. style. theme_dark );
Setcontentview (R. layout. maind );

Button BTN = (button) findviewbyid (R. Id. btntest );
BTN. setonclicklistener (this );
}
 
@ Override
Public void onclick (view arg0 )...{
// Use an explicit intent to start the service
Intent I = new intent ();
I. setclass (this, hellotwodservice. Class );
// Add my name
Bundle B = new bundle ();
B. putstring ("name", "sharetop ");
This. startservice (I, B );
}
 
}

Of course, to start this hellotwod, you also need to add a little code in my first hellotwo (I won't bother ). Let's see how the hellotwodservice is implemented:

Public class hellotwodservice extends service ...{
Public timer;
Public final string tag = "hellotwodservice_tag ";
Public void oncreate ()...{
Super. oncreate ();

Log. D (TAG, "oncreate ");

Timer = new timer (true );

}
@ Override
Public ibinder getbinder ()...{
// Todo auto-generated method stub
Return NULL;
}
Public void onstart (INT startid, bundle Arg)
...{
// Check what startid is.
If (Arg! = NULL)
Log. D (TAG, "onstart" + integer. valueof (startid). tostring () + "from" + Arg. getstring ("name "));
Else
Log. D (TAG, "onstart with null bundle ");

Timer. Schedule (New timertask ()...{
Public void run ()...{
// Indicates my existence
Log. D (TAG, "say from a timer .");
// Stop your service
Hellotwodservice. This. stopself ();
}
},5000 );
}
Public void ondestroy ()
...{
Log. D (TAG, "ondestroy ");
}
}

Here, I use a timer to delay the display of messages for 5 seconds. Otherwise, it will be displayed immediately and I don't think it is like a background service. Use the log to output the startid in the onstart to check whether it is an identifier.

Here is a simple icationicationmanager. After reading the API documentation, I think the simplest thing is probably the icationicationmanager. yywithtext (). The above run method is modified as follows:

Timer. Schedule (New timertask ()...{
Public void run ()...{

Icationicationmanager manager = (notificationmanager) getsystemservice (notification_service );
Manager. policywithtext (1001, "zookeeper sharetop zookeeper. ", Icationicationmanager. length_long, null );

Hellotwodservice. This. stopself ();
}
},5000 );

Try again. It is too simple. Notification is mainly used for the background service to notify the front-end. Therefore, Android provides three different notification methods. notifywithtext can simply display a string, while yywithview is a little complicated, you can have a view to construct the display information box. The most flexible one is the consumer y (int id, Notification). The parameter notification is an instance of notification type.

Modify the run method as follows:

Timer. Schedule (New timertask ()...{
Public void run ()...{

Icationicationmanager manager = (notificationmanager) getsystemservice (notification_service );
Notification NF = new notification (R. drawable. Icon, "This is a detailed description of information", null, "Information title", null );
Manager. Sort y (0, NF );

Hellotwodservice. This. stopself ();
}
},5000 );

Create a notification instance NF. The first parameter of the constructor is the one that is displayed in the status bar (that is, the one on the Android mobile phone that displays signal strength, battery, and other information). There can be

A title and detailed information after a click. This is a string type, and an intent can be used to indicate a jump after a click.

OK, so far today. Next time you will experience various views.
 

Related Article

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.