Android Imitation 360 floating ball and boot from start development

Source: Internet
Author: User
Tags close page stub

Look at the picture below, the floating ball with the percent semicolon.

As shown in the top left corner of the picture, the ball will also show the current operation of the mobile phone, downward pull will also have the ejection to achieve acceleration, cleaning and other purposes.

Well, first realize a similar ball (only shapes, functions you know).

Check the relevant data, the entire interface in addition to the ball, the other parts are transparent treatment.

1, due to the use of compatmodewrapper, so you need to add the following permissions in Androidmanifest.xml:

<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>

To be started as an activity or service in an application, you need to register, here is the service:

<service android:name= ". Service1 "/>


2, the interface of transparent processing, the overall code is as follows:

Package com. Xxx.autostart;

Import Android.view.View;

Import Android.app.Service;
Import android.content.Intent;
Import Android.graphics.PixelFormat;
Import Android.os.Handler;
Import Android.os.IBinder;
Import android.view.Gravity;
Import Android.view.LayoutInflater;
Import android.view.MotionEvent;
Import Android.view.WindowManager;
Import Android.view.View.OnClickListener;
Import Android.view.View.OnTouchListener;
Import Android.view.WindowManager.LayoutParams;
Import Android.widget.ImageButton;
Import Android.widget.LinearLayout;

public class Service1 extends Service
{
LinearLayout mfloatlayout;
Windowmanager.layoutparams Wmparams;
WindowManager Mwindowmanager;

ImageButton Mfloatview;

@Override
public void OnCreate ()
{
TODO auto-generated Method Stub
Super.oncreate ();

Createfloatview ();
}

@Override
Public IBinder Onbind (Intent Intent)
{
TODO auto-generated Method Stub
return null;
}

private void Createfloatview ()
{
Wmparams = new Windowmanager.layoutparams ();

Mwindowmanager = (WindowManager) getapplication (). Getsystemservice (Getapplication (). Window_service);

Wmparams.type = Layoutparams.type_phone;
Wmparams.format = pixelformat.rgba_8888;

Wmparams.flags = layoutparams.flag_not_focusable;

wmparams.gravity = Gravity.left | Gravity.top;

wmparams.x = 0;
Wmparams.y = 0;

Wmparams.width = WindowManager.LayoutParams.WRAP_CONTENT;
Wmparams.height = WindowManager.LayoutParams.WRAP_CONTENT;

Layoutinflater inflater = Layoutinflater.from (Getapplication ());
Mfloatlayout = (linearlayout) inflater.inflate (r.layout.float_layout, NULL);

Mwindowmanager.addview (Mfloatlayout, wmparams);

Mfloatview = (ImageButton) Mfloatlayout.findviewbyid (r.id.float_id);

Mfloatlayout.measure (View.MeasureSpec.makeMeasureSpec (0),
View.MeasureSpec.UNSPECIFIED), View.measurespec
. Makemeasurespec (0, View.MeasureSpec.UNSPECIFIED));


Mfloatview.setontouchlistener (New Ontouchlistener () {
@Override
public boolean Ontouch (View V, motionevent event) {
TODO auto-generated Method Stub

wmparams.x = (int) event.getrawx ()-Mfloatview.getmeasuredwidth ()/2;

WMPARAMS.Y = (int) Event.getrawy ()-mfloatview.getmeasuredheight ()/2-25;

Mwindowmanager.updateviewlayout (Mfloatlayout, wmparams);
return false;
}
});

Mfloatview.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Mfloatview.setvisibility (view.invisible);

Handler Handler = new Handler ();
Handler.postdelayed (New Runnable () {
public void Run () {
Mfloatview.setvisibility (view.visible);
}
}, 3000);

}
});
}

@Override
public void OnDestroy ()
{
TODO auto-generated Method Stub
Super.ondestroy ();
if (mfloatlayout!= null)
{
Mwindowmanager.removeview (mfloatlayout);
}
}

}

Here is the use of ImageButton components to achieve the ball, the key is that its display of the picture is circular.

Therefore, to implement a floating window as a different shape, you only need to make the appropriate picture assigned to the component.

For the function of the small ball, just to achieve the mobile phone screen at random drag, click disappear three seconds after the recurrence.


3, floating small ball has, how to let it start? Note The class Service1 implemented above, and inherit the service.

In Maniactivity.java, the way to show it is simple, and the code is as follows:

Finish ();
Intent Intent = new Intent (Getapplicationcontext (), service1.class);
StartService (Intent);

Note that there is no need to do any processing for the Mainactivity class, and the default is good when you create a new project. Of course, there are other feature exceptions to implement.

Code finish (), add can not add, and then make the program to run only a small ball, the original interface let it disappear.

The effect is as follows, the figure is looking for a small red flame.




4, to this, floating small ball is realized, that how to let it boot from start?

In fact, it is also very simple, the use of broadcastreceiver.

Or do you want to add the permissions first:

<uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/>

In addition, for the system broadcast message, in order to be able to capture at the time of the boot, you need to add the following:

<receiver android:name= ". Bootbroadcastreceiver ">
<intent-filter>
<action android:name= "Android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

The implementation code is as follows:

Package com. Xxx.autostart;

Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;

public class Bootbroadcastreceiver extends Broadcastreceiver {

Static final String ACTION = "Android.intent.action.BOOT_COMPLETED";

@Override
public void OnReceive (context context, Intent Intent) {

if (Intent.getaction (). Equals (ACTION)) {
Intent intent1 = new Intent (context, service1.class);
Intent1.addflags (Intent.flag_activity_new_task);
Context.startservice (INTENT1);
}
}
}

Reboot the phone, you can find that the red ball will start on its own, but slower (compared to 360 security guards).





Android Boot from startup program instance

Background: When Android starts, it emits a system broadcast, the content is action_boot_completed, its word
String constants are represented as Android.intent.action.BOOT_COMPLETED. Just "capture" the message in the program, and then start the
Can. Remember, the Android framework says: Don ' t call me, I ' ll called you back. What we have to do is prepare to receive the message, and
The means of implementation is to achieve a broadcastreceiver.

1, interface Activity,bootstartdemo.java file

public class Bootstartdemo extends activity {
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
No title
Requestwindowfeature (Window.feature_no_title);
Fullscreen
GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Setcontentview (R.layout.main);
New Thread () {
public void Run () {
try {
/* 10 seconds to close the page * *
Sleep (10000);
catch (Exception e) {
E.printstacktrace ();
finally {
Finish (); Close page
}
}
}.start ();

}
}

This code is very simple, when the activity starts, will display TextView, use it to display the words you want to display, and this page only shows 10 seconds to disappear.

2. Receive broadcast message: Bootbroadcastreceiver.java

public class Bootbroadcastreceiver extends Broadcastreceiver {
Static final String action_boot= "Android.intent.action.BOOT_COMPLETED";

@Override
public void OnReceive (context context, Intent Intent) {
if (Intent.getaction (). Equals (Action_boot)) {
Intent ootstartintent=new Intent (context,bootstartdemo.class);
Ootstartintent.addflags (Intent.flag_activity_new_task);
Context.startactivity (ootstartintent);
}

}

}

The class continues to be broadcastreceiver, in OnReceive of the method, to detect whether the received Intent meets
Boot_completed, if fit, initiates the activity of Bootstartdemo.

3. configuration file

(1) Androidmanifest.xml:

<?xml version= "1.0" encoding= "Utf-8"?>
<!--This is a power-on self starter program-->
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.ajie.bootstartdemo"
Android:versioncode= "1"
Android:versionname= "1.0" >
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<activity android:name= ". Bootstartdemo "
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<span style= "color: #ff00ff;" ><receiver android:name= ". Bootbroadcastreceiver ">
<intent-filter>
<action android:name= "Android.intent.action.BOOT_COMPLETED"/>
<category android:name= "Android.intent.category.HOME"/>
</intent-filter>
</receiver>
</span> </application>
<span style= "color: #ff00ff;" ><strong><uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED" ></ Uses-permission></strong>
</span></manifest>

Notice that part of the color red, the node registers a receiver with the system, and the child node Intent-filter represents the receiving
Android.intent.action.BOOT_COMPLETED message. And also configure Android.permission.RECEIVE_BOOT_COMPLETED permissions.

(2) Layout file, Main.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"

>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:text= "@string/boottext"
Android:textcolor= "#5F2DD2"
Android:background= "#FFFFFF"
Android:textsize= "60px"
Android:gravity= "Center_horizontal"
/>
</LinearLayout>

When finished, compile the APK package and install it into the simulator or mobile phone. Shut down and reboot, and it will show the page Bootstartdemo this activity.

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.