首页 新闻 会员 周边 捐助

自定义的Toast为什么添加ontouchListener和点击事件无响应。。

0
悬赏园豆:20 [已关闭问题] 关闭于 2015-07-25 08:34
  1 package com.txy.mobliesafe.service;
  2 
  3 import com.txy.mobliesafe.R;
  4 import com.txy.mobliesafe.db.dao.NumberSelectUtils;
  5 
  6 import android.app.Service;
  7 import android.content.BroadcastReceiver;
  8 import android.content.Context;
  9 import android.content.Intent;
 10 import android.content.IntentFilter;
 11 import android.content.SharedPreferences;
 12 import android.content.SharedPreferences.Editor;
 13 import android.graphics.Color;
 14 import android.graphics.PixelFormat;
 15 import android.os.IBinder;
 16 import android.telephony.PhoneStateListener;
 17 import android.telephony.TelephonyManager;
 18 import android.util.Log;
 19 import android.view.MotionEvent;
 20 import android.view.View;
 21 import android.view.View.OnClickListener;
 22 import android.view.View.OnTouchListener;
 23 import android.view.WindowManager;
 24 import android.widget.TextView;
 25 
 26 public class AddressService extends Service {
 27 
 28     private static final String TAG = "AddressService";
 29     TelephonyManager tm;
 30     MyListener listener;
 31     OutCallReceiver receiver;
 32     WindowManager wm;
 33     SharedPreferences sp;
 34     // TextView tv_Toast;
 35     View toastView;
 36     WindowManager.LayoutParams params = new WindowManager.LayoutParams();
 37 
 38     @Override
 39     public IBinder onBind(Intent arg0) {
 40         return null;
 41     }
 42 
 43     @Override
 44     public void onCreate() {
 45         super.onCreate();
 46         tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
 47         MyListener listener = new MyListener();
 48         tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
 49 
 50         receiver = new OutCallReceiver();
 51         IntentFilter filter = new IntentFilter();
 52         filter.addAction("android.intent.action.NEW_OUTGOING_CALL");
 53         registerReceiver(receiver, filter);
 54 
 55         wm = (WindowManager) getSystemService(WINDOW_SERVICE);
 56 
 57         sp = getSharedPreferences("config", MODE_PRIVATE);
 58     }
 59 
 60     class OutCallReceiver extends BroadcastReceiver {
 61 
 62         @Override
 63         public void onReceive(Context context, Intent arg1) {
 64             String number = getResultData();
 65             String addr = NumberSelectUtils.getAddr(number);
 66             // Toast.makeText(context.getApplicationContext(), addr,
 67             // Toast.LENGTH_SHORT).show();
 68             myToast(addr);
 69         }
 70 
 71     }
 72 
 73     @Override
 74     public void onDestroy() {
 75         super.onDestroy();
 76 
 77         tm.listen(listener, PhoneStateListener.LISTEN_NONE);
 78         listener = null;
 79 
 80         unregisterReceiver(receiver);
 81         receiver = null;
 82 
 83         toastView = null;
 84     }
 85 
 86     public void myToast(String addr) {
 87         /*
 88          * tv_Toast = new TextView(getApplicationContext());
 89          * tv_Toast.setTextSize(25); tv_Toast.setText(addr);
 90          */
 91         int[] bgitems = { R.drawable.call_locate_white,
 92                 R.drawable.call_locate_orange, R.drawable.call_locate_blue,
 93                 R.drawable.call_locate_gray, R.drawable.call_locate_green };
 94         // 自定义吐司
 95         toastView = View.inflate(getApplicationContext(),
 96                 R.layout.toast_call_addr, null);
 97 
 98         TextView tv_Toast = (TextView) toastView.findViewById(R.id.tv_addr);
 99         int i = sp.getInt("background", 0);
100         Log.i(TAG, i + "");
101         toastView.setBackgroundResource(bgitems[i]);
102         MyTouchListener myTouchListener = new MyTouchListener();
103         toastView.setOnTouchListener(myTouchListener);
104         tv_Toast.setTextColor(Color.BLACK);
105         tv_Toast.setText(addr);
106         /*
107          * toastView.setOnClickListener(new OnClickListener() {
108          * 
109          * @Override public void onClick(View v) { Log.i(TAG, "被点击"); } });
110          * Log.i(TAG, "设置监听器"); toastView.setOnTouchListener(new
111          * OnTouchListener() { int posX; int posY;
112          * 
113          * @Override public boolean onTouch(View v, MotionEvent event) { switch
114          * (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.i(TAG,
115          * "手指摁下"); posX = (int) event.getRawX(); posY = (int) event.getRawY();
116          * break; case MotionEvent.ACTION_MOVE: Log.i(TAG, "手指移动"); int newX =
117          * (int) event.getRawX(); int newY = (int) event.getRawY(); int dX =
118          * newX - posX;// 每次移动的偏移量 int dY = newY - posY; params.x += dX;
119          * params.y += dY; // 边界问题 if (params.x < 0) { params.x = 0; } if
120          * (params.y < 0) { params.y = 0; } if (params.x >
121          * (wm.getDefaultDisplay().getWidth() - toastView .getWidth())) {
122          * params.x = (wm.getDefaultDisplay().getWidth() - toastView
123          * .getWidth()); } if (params.y > (wm.getDefaultDisplay().getHeight() -
124          * toastView .getHeight())) { params.y =
125          * (wm.getDefaultDisplay().getHeight() - toastView .getHeight()); }
126          * wm.updateViewLayout(toastView, params); // 保存 posX = (int)
127          * event.getRawX(); posY = (int) event.getRawY(); break; case
128          * MotionEvent.ACTION_UP: Log.i(TAG, "手指放开"); Editor edit = sp.edit();
129          * edit.putInt("lastX", params.x); edit.putInt("lastY", params.y);
130          * edit.commit(); break;
131          * 
132          * default: break; } return true; } });
133          */
134         params.height = WindowManager.LayoutParams.WRAP_CONTENT;
135         params.width = WindowManager.LayoutParams.WRAP_CONTENT;
136         params.x = sp.getInt("lastX", 100);
137         params.y = sp.getInt("lastY", 100);
138         params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
139                 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
140                 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
141         params.format = PixelFormat.TRANSLUCENT;
142         params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;// TYPE_TOAST--toast类型不可以移动,自定义成TYPE_PRIORITY_PHONE需要权限SYSTEM_ALERT_WINDOW
143         wm.addView(toastView, params);
144 
145     }
146 
147     public class MyTouchListener implements View.OnTouchListener {
148 
149         int posX;
150         int posY;
151 
152         @Override
153         public boolean onTouch(View v, MotionEvent event) {
154             switch (event.getAction()) {
155             case MotionEvent.ACTION_DOWN:
156                 Log.i(TAG, "手指摁下");
157                 posX = (int) event.getRawX();
158                 posY = (int) event.getRawY();
159                 break;
160             case MotionEvent.ACTION_MOVE:
161                 Log.i(TAG, "手指移动");
162                 int newX = (int) event.getRawX();
163                 int newY = (int) event.getRawY();
164                 int dX = newX - posX;// 每次移动的偏移量
165                 int dY = newY - posY;
166                 params.x += dX;
167                 params.y += dY;
168                 // 边界问题
169                 if (params.x < 0) {
170                     params.x = 0;
171                 }
172                 if (params.y < 0) {
173                     params.y = 0;
174                 }
175                 if (params.x > (wm.getDefaultDisplay().getWidth() - toastView
176                         .getWidth())) {
177                     params.x = (wm.getDefaultDisplay().getWidth() - toastView
178                             .getWidth());
179                 }
180                 if (params.y > (wm.getDefaultDisplay().getHeight() - toastView
181                         .getHeight())) {
182                     params.y = (wm.getDefaultDisplay().getHeight() - toastView
183                             .getHeight());
184                 }
185                 wm.updateViewLayout(toastView, params);
186                 // 保存
187                 posX = (int) event.getRawX();
188                 posY = (int) event.getRawY();
189                 break;
190             case MotionEvent.ACTION_UP:
191                 Log.i(TAG, "手指放开");
192                 Editor edit = sp.edit();
193                 edit.putInt("lastX", params.x);
194                 edit.putInt("lastY", params.y);
195                 edit.commit();
196                 break;
197 
198             default:
199                 break;
200             }
201             return false;
202         }
203     }
204 
205     public class MyListener extends PhoneStateListener {
206         @Override
207         public void onCallStateChanged(int state, String incomingNumber) {
208             super.onCallStateChanged(state, incomingNumber);
209             switch (state) {
210             case TelephonyManager.CALL_STATE_RINGING:
211                 String addr = NumberSelectUtils.getAddr(incomingNumber);
212                 if (toastView != null) {
213                     myToast(addr);
214                 }
215                 break;
216             case TelephonyManager.CALL_STATE_IDLE:
217                 if (toastView != null) {
218                     wm.removeView(toastView);
219                 }
220                 break;
221             default:
222                 break;
223             }
224         }
225     }
226 
227 }
AddressService

求助~~就是可以显示出来自定义的Toast,但是当点击他的时候一点响应都没有。。

night-tian的主页 night-tian | 菜鸟二级 | 园豆:204
提问于:2015-07-23 21:02
< >
分享
所有回答(1)
0

| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE  删除即可

night-tian | 园豆:204 (菜鸟二级) | 2015-07-25 08:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册