Android uses Broadcastreceiver to realize the function of the screen when the phone is powered on _android

Source: Internet
Author: User

This article is an example of Android using Broadcastreceiver to realize the function of displaying the screen after the phone is powered on. Share to everyone for your reference, specific as follows:

In this example, how to use Broadcastrececiver is described. Use Broadcastreceiver to inherit this class, and override the public void Onreceiver (context context,intent Intent) method to add the functionality you want to implement in this method.

The following is an example of a screen that automatically displays when the Android phone is powered on, introducing a picture through ImageView to show a greeting through TextView. It automatically displays every time the phone is powered on.

The specific procedures are shown below:

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.ImageView;
Import Android.widget.TextView;
public class A01activity extends activity {
 private TextView TV;
 Private ImageView IV;
  /** called the activity is a. * *
  @Override public
  void OnCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.main);
    tv= (TextView) Findviewbyid (r.id.tv);
    Tv.settext ("Welcome to 168855");
    iv= (ImageView) Findviewbyid (R.ID.IV);
    Iv.setbackgroundresource (R.DRAWABLE.A);    
  }


Then write a class in the same package to inherit the Broadcastreceiver and rewrite the public void Onreceiver (the context, Intent Intent) method, which is as follows:

Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
public class A01_01 extends broadcastreceiver{
 @Override public
 void OnReceive (context arg0, Intent arg1) {
 TODO auto-generated Method Stub
 Intent i=new Intent (arg0,a01activity.class);
 I.setflags (intent.flag_activity_new_task);
 Arg0.startactivity (i);
 }
}

In order to capture the Power-on action information that is broadcast on the system boot, you must set the <receiver> mark in the <application> in Androidmanifest.xml. and specify the Intent-filter name as Android.intent.action.BOOT_COMPLETED. The Androidmanifest.xml is shown below:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Com.my.a01 "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkve
    Rsion= "/>" <application android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" > <activity android:name= ". A01activity "android:label=" @string/app_name "> <intent-filter> <action android:name=" Andr Oid.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent -filter> </activity> <receiver android:name= "a01_01" > <intent-filter> <actio n android:name= "Android.intent.action.BOOT_COMPLETED" ></action> <category android:name= "Android.inten T.category.home "></category> </intent-filter> </receiver> </application> </manif est&Gt

 

More interested readers of Android-related content can view this site: "Android Layout Layout Skills Summary", "Android View tips Summary" and "Android Development introduction and Advanced Course"

I hope this article will help you with the Android program.

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.