Android development: the system automatically enables startup.

Source: Internet
Author: User

We have already talked about Android development: freely choosing TextView text and how to implement TCP and UDP transmission. If you are developing an application that needs to update data in real time and remind users to view new data when there is new data, you need to start a Service in the background, then, you can obtain data from the network in real time. However, if the user shuts down and restarts, your Service may disappear! So how can we ensure that your Service is still active after the boot, and your mobile phone secretly obtains data from the network?

It is very simple. We only need to enable auto-start upon startup. Android auto-start may be the simplest in the mobile operating system. We only need to listen to a Broadcast started upon startup. First, write a Receiver, that is, the broadcast listener), and inherit the BroadcastReceiver, as shown below:

 
 
  1. Public class BootReceiver extends BroadcastReceiver {
  2. Private PendingIntent mAlarmSender;
  3. @ Override
  4. Public void onReceive (Context context, Intent intent ){
  5. // Start a Service, Activity, and so on. In this example, start a scheduled scheduling program and start a Service every 30 minutes to update data.
  6. MAlarmSender = PendingIntent. getService (context, 0, new Intent (context,
  7. RefreshDataService. class), 0 );
  8. Long firstTime = SystemClock. elapsedRealtime ();
  9. AlarmManager am = (AlarmManager) context
  10. . GetSystemService (Activity. ALARM_SERVICE );
  11. Am. cancel (mAlarmSender );
  12. Am. setRepeating (AlarmManager. ELAPSED_REALTIME_WAKEUP, firstTime,
  13. 30x60*1000, mAlarmSender );
  14. }
  15. }

Next, we only need to register this handler in the application configuration file AndroidManifest. xml to listen to system startup events, as shown below:

 
 
  1. <Cycler android: name = ". service. BootReceiver">
  2. <Intent-filter>
  3. <! -- Called after the system is started -->
  4. <Action android: name = "android. intent. action. BOOT_COMPLETED">
  5. </Action>
  6. </Intent-filter>
  7. </Cycler>

In this way, the system is automatically started. How can this problem be solved? Is it easy?

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.