編輯:關於Android編程
最近給老大做個安卓手機的小程序,希望實現 來去電的時候,提示一下(主要是看看有沒有這個號碼的相關事情)。
在網上找了很多,都是只有來電的懸浮窗。
算了,還是自己研究吧。
下面給出監聽部分和懸浮窗,可拖拽的。
[java]
//來去電廣播
public class InterceptCall extends BroadcastReceiver {
private static final String TAG = "PhoneStatReceiver";
private WindowManager wm;
WindowManager.LayoutParams params;
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
private Button tv;
Date curDate = null;
Context mcontext;
private void updateViewPosition() {
// 更新浮動窗口位置參數
params.x = (int) (x - mTouchStartX);
params.y = (int) (y - mTouchStartY);
wm.updateViewLayout(tv, params);
}
@Override
public void onReceive(Context context, Intent intent) {
mcontext = context;
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
// 如果是去電(撥出)
// mCallEvent = CALL_EVENT_OUTGOING;
MyApplication.NumberType = 1;
String mCallNumber = intent
.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(TAG, "call OUT:" + mCallNumber);
MyApplication.PhoneNum = mCallNumber;
wm = null;
wm = (WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
// WindowManager.LayoutParams
params = new WindowManager.LayoutParams();
params.type = LayoutParams.TYPE_PHONE;
// 設置Window flag
params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
params.gravity = Gravity.LEFT | Gravity.TOP; // 調整懸浮窗口至左上角 <SPAN style="COLOR: #ff0000">為實現拖拽,必須加上這個,不然會漂移,不信你試試。</SPAN>
// 以屏幕左上角為原點,設置x、y初始
params.x = 20;
params.y = 5;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.RGBA_8888;
tv = new Button(context);
tv.setText("雙擊查看:\n" + mCallNumber + "的記錄");// +sds);
wm.addView(tv, params);
tv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// 獲取相對屏幕的坐標,即以屏幕左上角為原點
x = event.getRawX();
y = event.getRawY() - 25; // 25是系統狀態欄的高度
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 獲取相對View的坐標,即以此View左上角為原點
mTouchStartX = event.getX();
mTouchStartY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
updateViewPosition();
break;
// 判斷手勢點擊起來的時候:
case MotionEvent.ACTION_UP:
updateViewPosition();
mTouchStartX = mTouchStartY = 0;
if (curDate != null) {
Date dateNow = new Date(System.currentTimeMillis());
// date轉成毫秒
long diff = dateNow.getTime() - curDate.getTime();
// 這樣得到的差值是微秒級別
if (diff < 400) {
// 雙擊
try {
if (wm != null) {
wm.removeView(tv);
}
} catch (Exception e) {
}
curDate = null;
Intent service = new Intent();
service.setClass(mcontext, TeleLogContent.class);
service.putExtra("PhoneNumer",
MyApplication.PhoneNum);
service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mcontext.startActivity(service);
} else {
// 單擊
curDate = new Date(System.currentTimeMillis());
}
// 這裡也是單擊
} else {
// 單擊
curDate = new Date(System.currentTimeMillis());
}
break;
}
return true;
}
});
// AskCallStateThread callStateThread = new
// AskCallStateThread(context);
// callStateThread.start();
} else {
String comeInNum = intent
.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.out.println("來電號碼:" + comeInNum);
TelephonyManager telM = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
// 設置一個監聽器
telM.listen(new TelListener(context),
PhoneStateListener.LISTEN_CALL_STATE);
}
}
}
//來去電廣播
public class InterceptCall extends BroadcastReceiver {
private static final String TAG = "PhoneStatReceiver";
private WindowManager wm;
WindowManager.LayoutParams params;
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
private Button tv;
Date curDate = null;
Context mcontext;
private void updateViewPosition() {
// 更新浮動窗口位置參數
params.x = (int) (x - mTouchStartX);
params.y = (int) (y - mTouchStartY);
wm.updateViewLayout(tv, params);
}
@Override
public void onReceive(Context context, Intent intent) {
mcontext = context;
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
// 如果是去電(撥出)
// mCallEvent = CALL_EVENT_OUTGOING;
MyApplication.NumberType = 1;
String mCallNumber = intent
.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(TAG, "call OUT:" + mCallNumber);
MyApplication.PhoneNum = mCallNumber;
wm = null;
wm = (WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
// WindowManager.LayoutParams
params = new WindowManager.LayoutParams();
params.type = LayoutParams.TYPE_PHONE;
// 設置Window flag
params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
params.gravity = Gravity.LEFT | Gravity.TOP; // 調整懸浮窗口至左上角 為實現拖拽,必須加上這個,不然會漂移,不信你試試。
// 以屏幕左上角為原點,設置x、y初始
params.x = 20;
params.y = 5;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.RGBA_8888;
tv = new Button(context);
tv.setText("雙擊查看:\n" + mCallNumber + "的記錄");// +sds);
wm.addView(tv, params);
tv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// 獲取相對屏幕的坐標,即以屏幕左上角為原點
x = event.getRawX();
y = event.getRawY() - 25; // 25是系統狀態欄的高度
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 獲取相對View的坐標,即以此View左上角為原點
mTouchStartX = event.getX();
mTouchStartY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
updateViewPosition();
break;
// 判斷手勢點擊起來的時候:
case MotionEvent.ACTION_UP:
updateViewPosition();
mTouchStartX = mTouchStartY = 0;
if (curDate != null) {
Date dateNow = new Date(System.currentTimeMillis());
// date轉成毫秒
long diff = dateNow.getTime() - curDate.getTime();
// 這樣得到的差值是微秒級別
if (diff < 400) {
// 雙擊
try {
if (wm != null) {
wm.removeView(tv);
}
} catch (Exception e) {
}
curDate = null;
Intent service = new Intent();
service.setClass(mcontext, TeleLogContent.class);
service.putExtra("PhoneNumer",
MyApplication.PhoneNum);
service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mcontext.startActivity(service);
} else {
// 單擊
curDate = new Date(System.currentTimeMillis());
}
// 這裡也是單擊
} else {
// 單擊
curDate = new Date(System.currentTimeMillis());
}
break;
}
return true;
}
});
// AskCallStateThread callStateThread = new
// AskCallStateThread(context);
// callStateThread.start();
} else {
String comeInNum = intent
.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.out.println("來電號碼:" + comeInNum);
TelephonyManager telM = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
// 設置一個監聽器
telM.listen(new TelListener(context),
PhoneStateListener.LISTEN_CALL_STATE);
}
}
}
以上主要是去電部分的提示框。(第一次寫android 的程序,所以經驗不足啊。見笑了)
下面是來電部分:
[java]
import java.util.Date;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
//監聽來電
public class TelListener extends PhoneStateListener {
private Context context;
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
int type = 0;
private WindowManager wm;
WindowManager.LayoutParams params;
private Button tv;
int listview = 0;
int calli = 1;
public TelListener(Context context) {
this.context = context;
}
private void updateViewPosition() {
// 更新浮動窗口位置參數
params.x = (int) (x - mTouchStartX);
params.y = (int) (y - mTouchStartY);
wm.updateViewLayout(tv, params);
}
Date curDate = null;
String phoneNumber = "";
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// 掛斷
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// 接聽
break;
case TelephonyManager.CALL_STATE_RINGING:
// 來電
MyApplication.NumberType = 2;
if (MyApplication.isCloses == 0) {
MyApplication.isCloses = 1;
MyApplication.PhoneNum = incomingNumber;
wm = null;
wm = (WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
params = new WindowManager.LayoutParams();
params.type = LayoutParams.TYPE_PHONE;
params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
params.gravity = Gravity.LEFT | Gravity.TOP; // 調整懸浮窗口至左上角
// 以屏幕左上角為原點,設置x、y初始
params.x = 20;
params.y = 5;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.RGBA_8888;
tv = new Button(context);
tv.setText("雙擊查看\n" + incomingNumber + "的記錄");
wm.addView(tv, params);
tv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// 獲取相對屏幕的坐標,即以屏幕左上角為原點
x = event.getRawX();
y = event.getRawY() - 25; // 25是系統狀態欄的高度
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 獲取相對View的坐標,即以此View左上角為原點
mTouchStartX = event.getX();
mTouchStartY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
updateViewPosition();
break;
// 判斷手勢點擊起來的時候:
case MotionEvent.ACTION_UP:
updateViewPosition();
mTouchStartX = mTouchStartY = 0;
if (curDate != null) {
Date dateNow = new Date(System
.currentTimeMillis());
// date轉成毫秒
long diff = dateNow.getTime()
- curDate.getTime();
// 這樣得到的差值是微秒級別
if (diff < 400) {
// 雙擊
try {
if (wm != null) {
wm.removeView(tv);
MyApplication.isCloses=0;
}
} catch (Exception e) {
}
curDate = null;
Intent service = new Intent();
service.setClass(context,
TeleLogContent.class);
service.putExtra("PhoneNumer",
MyApplication.PhoneNum);
service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(service);
} else {
// 單擊
curDate = new Date(System
.currentTimeMillis());
}
// 這裡也是單擊
} else {
// 單擊
curDate = new Date(System.currentTimeMillis());
}
break;
}
return true;
}
});
}
break;
}
}
}
import java.util.Date;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
//監聽來電
public class TelListener extends PhoneStateListener {
private Context context;
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
int type = 0;
private WindowManager wm;
WindowManager.LayoutParams params;
private Button tv;
int listview = 0;
int calli = 1;
public TelListener(Context context) {
this.context = context;
}
private void updateViewPosition() {
// 更新浮動窗口位置參數
params.x = (int) (x - mTouchStartX);
params.y = (int) (y - mTouchStartY);
wm.updateViewLayout(tv, params);
}
Date curDate = null;
String phoneNumber = "";
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// 掛斷
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// 接聽
break;
case TelephonyManager.CALL_STATE_RINGING:
// 來電
MyApplication.NumberType = 2;
if (MyApplication.isCloses == 0) {
MyApplication.isCloses = 1;
MyApplication.PhoneNum = incomingNumber;
wm = null;
wm = (WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
params = new WindowManager.LayoutParams();
params.type = LayoutParams.TYPE_PHONE;
params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
params.gravity = Gravity.LEFT | Gravity.TOP; // 調整懸浮窗口至左上角
// 以屏幕左上角為原點,設置x、y初始
params.x = 20;
params.y = 5;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.RGBA_8888;
tv = new Button(context);
tv.setText("雙擊查看\n" + incomingNumber + "的記錄");
wm.addView(tv, params);
tv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// 獲取相對屏幕的坐標,即以屏幕左上角為原點
x = event.getRawX();
y = event.getRawY() - 25; // 25是系統狀態欄的高度
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 獲取相對View的坐標,即以此View左上角為原點
mTouchStartX = event.getX();
mTouchStartY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
updateViewPosition();
break;
// 判斷手勢點擊起來的時候:
case MotionEvent.ACTION_UP:
updateViewPosition();
mTouchStartX = mTouchStartY = 0;
if (curDate != null) {
Date dateNow = new Date(System
.currentTimeMillis());
// date轉成毫秒
long diff = dateNow.getTime()
- curDate.getTime();
// 這樣得到的差值是微秒級別
if (diff < 400) {
// 雙擊
try {
if (wm != null) {
wm.removeView(tv);
MyApplication.isCloses=0;
}
} catch (Exception e) {
}
curDate = null;
Intent service = new Intent();
service.setClass(context,
TeleLogContent.class);
service.putExtra("PhoneNumer",
MyApplication.PhoneNum);
service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(service);
} else {
// 單擊
curDate = new Date(System
.currentTimeMillis());
}
// 這裡也是單擊
} else {
// 單擊
curDate = new Date(System.currentTimeMillis());
}
break;
}
return true;
}
});
}
break;
}
}
}
前幾天,我們客戶端這邊收到了市場部的一個需求,需要在我們訂單成交後,我們的客戶端有一個上傳交易憑證的功能,那麼如何在Android實現上傳圖片的這個功能呢?在我進行編碼之
前些天在展訊6825C 上調試gc2115攝像頭,發現後攝顯示效果非常的差,出現很嚴重的整個預覽界面豎條紋現象,但是對光線變化還是有反應的,初步判斷是sens
今天給大家帶來一個向右滑動銷毀Activity的效果,Activtiy隨著手指的移動而移動,該效果在Android應用中還是比較少見的,在IOS中就比較常見了,例如“網易
前段時間在寫直播的時候,需要觀眾在看直播的時候點贊的效果,在此參照了騰訊大神寫的點贊(飄心動畫效果)。下面是效果圖:1.自定義飄心動畫的屬性在attrs.xml 中增加自