Create a drag effect on the previous custom toast, and double-click the Center effect

Source: Internet
Author: User

Attention:

/*toast form type does not respond to form click events by default

Params.type = windowmanager.layoutparams.type_toast;*/
/* Create a form type with phone priority and need to add permissions <uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>*/


Params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;

The code is as follows:

Package com.zaizai.safty.service;

Import Android.app.Service;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import android.content.SharedPreferences;
Import Android.graphics.PixelFormat;
Import Android.os.IBinder;
Import Android.os.SystemClock;
Import Android.telephony.PhoneStateListener;
Import Android.telephony.TelephonyManager;
Import Android.util.Log;
Import android.view.Gravity;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.WindowManager;
Import Android.widget.TextView;


Import COM.ZAIZAI.SAFTY.R;
Import Com.zaizai.safty.db.dao.NumberAddressQueryUtils;

public class Addressservice extends Service {

private static final String TAG = "Zaizai";
Form Manager
Private WindowManager WindowManager;
Private view view;

/**
* Telephone
*/

Private Telephonymanager Telephonymanager;
Private Mylistenerphone Mylistenerphone;

Private Outcallreceiver Outcallreceiver;
Private Windowmanager.layoutparams params;
Private Sharedpreferences sharedpreferences;

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

Internal classes inside the service
The life cycle of the broadcast receiver is the same as the service

Class Outcallreceiver extends Broadcastreceiver {

@Override
public void OnReceive (context context, Intent Intent) {
This is the number we got on the broadcast.
String phone = Getresultdata ();
Querying the database
String address = numberaddressqueryutils.querynumber (phone);

Toast.maketext (context, address, toast.length_long). Show ();
Mytoast (address);
}

}

Private class Mylistenerphone extends Phonestatelistener {

@Override
public void oncallstatechanged (int state, String incomingnumber) {
State: status, Incomingnumber: Call number
Super.oncallstatechanged (state, Incomingnumber);
Switch (state) {
Case telephonymanager.call_state_ringing://Call Ringing
Querying the operation of the database
String address = Numberaddressqueryutils
. Querynumber (Incomingnumber);
Toast.maketext (Getapplicationcontext (), address, Toast.length_long). Show ();
Mytoast (address);
Break

Case telephonymanager.call_state_idle://Phone idle status: Hang up phone, call reject
To remove this view
if (view! = null) {
Windowmanager.removeview (view);
}
Break

Default
Break
}
}

}

@Override
public void OnCreate () {
TODO auto-generated Method Stub
Super.oncreate ();
Telephone service
Telephonymanager = (Telephonymanager) getsystemservice (Telephony_service);

Listen to incoming Calls
Mylistenerphone = new Mylistenerphone ();
Registration service to listen to the phone
Telephonymanager.listen (Mylistenerphone, phonestatelistener.listen_call_state);

Use code to register the broadcast receiver begin
Outcallreceiver = new Outcallreceiver ();
Intentfilter filter = new Intentfilter ();
Filter.addaction ("Android.intent.action.NEW_OUTGOING_CALL");
Registerreceiver (outcallreceiver, filter);
Use code to register the broadcast receiver end
Instantiating a form
WindowManager = (WindowManager) getsystemservice (Window_service);
}

/**
* Custom Toast
*
* @param address
*/

Private long[] mHITs = new long[2];

public void Mytoast (String address) {
/* Draw UI Interface */
View = View.inflate (this, r.layout.address_show, null);
TextView TextView = (TextView) View.findviewbyid (r.id.tv_address);
View.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {

/* Double-click Center two times */
System.arraycopy (mhits, 1, mhits, 0, mhits.length-1);
Mhits[mhits.length-1] = Systemclock.uptimemillis ();

if (Mhits[0] >= (Systemclock.uptimemillis ()-500)) {
/* Successfully double-click two times */
LOG.I (TAG, "Double click Success:");
Params.x = Windowmanager.getdefaultdisplay (). GetWidth ()/2-view.getwidth ()/2;
Params.y = Windowmanager.getdefaultdisplay (). GetHeight ()/2-view.getheight ()/2;
Windowmanager.updateviewlayout (view, params);
Sharedpreferences.editor Editor = Sharedpreferences.edit ();
Editor.putint ("Lastx", params.x);
Editor.putint ("Lasty", params.y);
Editor.commit ();

}
}
});

View.setontouchlistener (New View.ontouchlistener () {
int StartX;
int starty;

@Override
public boolean OnTouch (View V, motionevent event) {

Switch (event.getaction ()) {
Case motionevent.action_down://Finger Press the screen
{
StartX = (int) event.getrawx ();
Starty = (int) Event.getrawy ();
LOG.I (TAG, "start position:" + StartX + "," + starty);
Break
}
Case motionevent.action_move://finger moves on the screen
{
int newx = (int) event.getrawx ();
int newy = (int) Event.getrawy ();
LOG.I (TAG, "mobile post position:" + newx + "," + newy);
int dx = NEWX-STARTX;
int dy = Newy-starty;
LOG.I (TAG, "shifted offset after:" + dx + "," + dy);
LOG.I (TAG, "Update ImageView offset in the form:" + dx + "," + dy);
/* Reset the form start position, end position */
Params.x + = DX;
Params.y + = dy;
if (Params.x < 0) {
params.x = 0;
} else if (Params.y < 0) {
Params.y = 0;
} else if (Params.x > (Windowmanager.getdefaultdisplay (). GetWidth ()-view.getwidth ())) {
Params.x = Windowmanager.getdefaultdisplay (). GetWidth ()-view.getwidth ();
} else if (Params.y > (Windowmanager.getdefaultdisplay (). GetHeight ()-view.getheight ())) {
Params.y = Windowmanager.getdefaultdisplay (). GetHeight ()-view.getheight ();
}
Windowmanager.updateviewlayout (view, params);
Reinitialize the start end position of the finger.
StartX = (int) event.getrawx ();
Starty = (int) Event.getrawy ();
Break
}
Case motionevent.action_up://finger off screen
{
Record the coordinates of the control from the upper-left corner of the screen
Sharedpreferences.editor Editor = Sharedpreferences.edit ();
Editor.putint ("Lastx", params.x);
Editor.putint ("Lasty", params.y);
Editor.commit ();
Break
}

}
Return false;//event finishes, returns ture, intercepts events, and does not allow negative spatial layout to respond to touch events
}
});

"Translucent", "Vitality Orange", "Guardian Blue", "Metal Grey", "apple green"
int[] ids = {r.mipmap.call_locate_white9, r.mipmap.call_locate_orange9, R.mipmap.call_locate_blue9
, R.mipmap.call_locate_gray9, r.mipmap.call_locate_green9};

/* Get user-set style, default to 0 translucent */
Sharedpreferences = getsharedpreferences ("config", mode_private);

View.setbackgroundresource (Ids[sharedpreferences.getint ("which", 0)]);
Textview.settext (address);
The parameters of the form are set.
params = new Windowmanager.layoutparams ();

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

/* Toast Position setting params.gravity = Gravity.top; Multiple parameters with + sign connection */
params.gravity = Gravity.top + gravity.left;
/* Left 100 Top 100 */
Params.x = Sharedpreferences.getint ("Lastx", 0);
Params.y = Sharedpreferences.getint ("Lasty", 0);

Params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
/* | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
* Get rid of response time * *
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Params.format = pixelformat.translucent;
/*toast form Type default does not respond to form click events Params.type = windowmanager.layoutparams.type_toast;*/
/* Create a form type with phone priority and need to add permissions <uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>*/
Params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
Windowmanager.addview (view, params);

}

@Override
public void OnDestroy () {
TODO auto-generated Method Stub
Super.ondestroy ();
Cancel listening calls
Telephonymanager.listen (Mylistenerphone, Phonestatelistener.listen_none);
Mylistenerphone = null;

Unregister a broadcast recipient with code
Unregisterreceiver (Outcallreceiver);
Outcallreceiver = null;

}

}

Create a drag effect on the previous custom toast, and double-click the Center effect

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.