添加权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
private TextView mTextView01; private TextView mTextView03; private EditText mEditText1; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.phone_silent); /* 设定PhoneCallListener */ MyPhoneCallListener phoneListener = new MyPhoneCallListener(); /* 设定TelephonyManager去抓取Telephony Severice */ TelephonyManager telMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); /* 设定Listen Call */ telMgr.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); /* 设定找寻TextView、EditText */ mTextView01 = (TextView) findViewById(R.id.myTextView1); mTextView03 = (TextView) findViewById(R.id.myTextView3); mEditText1 = (EditText) findViewById(R.id.myEditText1); mEditText1.addTextChangedListener(new TextWatcher(){ public void afterTextChanged(Editable s) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { mTextView03.setText(mEditText1.getText()); } }); } /* 判断PhoneStateListener现在的状态 */ public class MyPhoneCallListener extends PhoneStateListener { public void onCallStateChanged(int state, String incomingNumber) { switch (state) { /* 判断手机目前是待机状态 */ case TelephonyManager.CALL_STATE_IDLE: mTextView01.setText("手机目前是待机状态"); try { AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); if (audioManager != null) { /* 设定手机为待机时,响铃模式为正常 */ audioManager .setRingerMode(AudioManager.RINGER_MODE_NORMAL); audioManager.getStreamVolume(AudioManager.STREAM_RING); } } catch (Exception e) { mTextView01.setText(e.toString()); e.printStackTrace(); } break; /* 判断状态为通话中 */ case TelephonyManager.CALL_STATE_OFFHOOK: mTextView01.setText("通话中"); break; /* 判断状态为来电 */ case TelephonyManager.CALL_STATE_RINGING: /* 显示现在来电的讯息 */ mTextView01.setText("来电:" + incomingNumber); /* 判断与输入电话是否为一样,当一样时响铃模式转为静音 */ if (incomingNumber.equals(mTextView03.getText().toString())) { try { AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); if (audioManager != null) { /* 设定响铃模式为静音 */ audioManager .setRingerMode(AudioManager.RINGER_MODE_SILENT); //audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);震动 audioManager .getStreamVolume(AudioManager.STREAM_RING); Toast.makeText(PhoneSilent.this, "静音模式", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { mTextView01.setText(e.toString()); e.printStackTrace(); break; } } } super.onCallStateChanged(state, incomingNumber); } }
首先,用到一个开源框架,可以去https://github.com/enormego/EGOTableViewPullRefresh下载。
然后,将里面的EGORefreshTableHeaderView.h和EGORefreshTableHeaderView.m拷到相应的项目里,在需要下拉刷新的页面对应的视图控制器中添加以下代码。
头文件:
#import "EGORefreshTableHeaderView.h" @interface RootViewController : UITableViewController <EGORefreshTableHeaderDelegate, UITableViewDelegate, UITableViewDataSource>{ EGORefreshTableHeaderView *_refreshHeaderView; BOOL _reloading; } - (void)reloadTableViewDataSource; - (void)doneLoadingTableViewData; @end
实现文件:
#import "RootViewController.h" @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; if (_refreshHeaderView == nil) { EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)]; view.delegate = self; [self.tableView addSubview:view]; _refreshHeaderView = view; [view release]; } [_refreshHeaderView refreshLastUpdatedDate]; } #pragma mark - #pragma mark UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 10; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 4; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } return cell; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [NSString stringWithFormat:@"Section %i", section]; } #pragma mark - #pragma mark Data Source Loading / Reloading Methods - (void)reloadTableViewDataSource{ _reloading = YES; } - (void)doneLoadingTableViewData{ _reloading = NO; [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView]; } #pragma mark - #pragma mark UIScrollViewDelegate Methods - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView]; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView]; } #pragma mark - #pragma mark EGORefreshTableHeaderDelegate Methods - (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{ [self reloadTableViewDataSource]; [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0]; } - (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{ return _reloading; } - (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{ return [NSDate date]; } #pragma mark - #pragma mark Memory Management - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)viewDidUnload { _refreshHeaderView=nil; } - (void)dealloc { _refreshHeaderView = nil; [super dealloc]; } @end
最后,一个很重要的步骤,别忘了加入QuartzCore.framework框架。
public boolean onTouchEvent(MotionEvent event) {
if(mAction == TOUCH_ACTION_MOVE){
return false;
}
boolean consumed = mGestureDetector.onTouchEvent(event);
// if (event.getAction() == MotionEvent.ACTION_UP) {
// if (mIsTouched || mIsDragging) {
// processScrollSnap();
// processGesture();
// }
// }
// return super.onTouchEvent(event);
return consumed;
}
public boolean dispatchTouchEvent(MotionEvent event) {
boolean consumed = mGestureDetector.onTouchEvent(event);
final int action = event.getAction();
Log.d("eeeee", "dispatchTouchEvent action="/blog_article/action/index.html", mAction="+mAction);
final float x = event.getX();
final float y = event.getY();
switch(action){
case MotionEvent.ACTION_DOWN:
mAction = TOUCH_ACTION_NONE;
mLastMotionX = x;
mLastMotionY = y;
break;
case MotionEvent.ACTION_MOVE:
int deltaX = (int) (mLastMotionX - x);
int deltaY = (int) (mLastMotionY - y);
if (mAction == TOUCH_ACTION_NONE) {
if (Math.abs(deltaX) < 10 && Math.abs(deltaY) > 10) {
mAction = TOUCH_ACTION_MOVE;
} else if (Math.abs(deltaX) > 10 && Math.abs(deltaY) < 10) {
mAction = TOUCH_ACTION_SNAP;
}
break;
}
if(mAction == TOUCH_ACTION_SNAP){
return true;
}else if(mAction == TOUCH_ACTION_MOVE){
return super.dispatchTouchEvent(event);
}
break;
case MotionEvent.ACTION_UP:
if (mIsTouched || mIsDragging) {
processScrollSnap();
processGesture();
return consumed;
}
mAction = TOUCH_ACTION_NONE;
default:
mAction = TOUCH_ACTION_NONE;
break;
}
// return consumed;
return super.dispatchTouchEvent(event);
}