Android Development Network and Touch problem solving method

Source: Internet
Author: User

Go ahead and tidy up some of the problems and techniques you've encountered in the Android development process.

1.4.0 or more versions of Android cannot be networked in the main thread
The Android application runs in a Dalvik virtual machine process, which starts with a main thread (Mainthread) that, in Android 4.0 or later, enforces time-consuming network operations on the main thread, and if network operations are required, You need to create additional threads or use Asynctask, as described below is another way to develop threads.

The code is as follows Copy Code

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.single);

New Thread (Postload). Start ();

}

Runnable postload = new Runnable () {

@Override
public void Run () {
TODO auto-generated Method Stub
To perform networking operations here
}
};

However, after networking operations generally need to update the UI based on networked data, a new problem arises, and the main thread is responsible for handling UI-related operations, so the main thread is called the UI thread. Android, which uses the UI single-threaded model, operates on the UI elements in the main thread, and the UI does not operate directly on it, and an error is made.

To solve this problem, you need to use the Android message loop mechanism to achieve communication between threads. That is, a non-UI thread sends a message to the UI thread, allowing the UI threads to manipulate the UI. For example, you can use the Post method of View to tell the UI thread to perform an operation to update the UI.

The code is as follows Copy Code

Runnable postload = new Runnable () {

@Override
public void Run () {
TODO auto-generated Method Stub
Internet Access data
TextView textshow = (TextView) Findviewbyid (r.id.show);
Textshow.post (New Runnable () {

@Override
public void Run () {//This method will execute on the UI thread
Textshow.settext (data);
}
});
}
};

2. Detect if there is a network

  code is as follows copy code

Final Connectivitymanager mconnectivity = (connectivitymanager) getsystemservice (Context.connectivity_service);
Check network connection, if no network is available, do not need to do networking operations, etc.
Networkinfo info = Mconnectivity.getactivenetworkinfo ();
if (info!= null) {//If there is a network
Determining the type of network connection
int nettype = Info.gettype ();
int netsubtype = Info.getsubtype ();

if (NetType = = Connectivitymanager.type_wifi) {
If the WIFI
else if (NetType = connectivitymanager.type_mobile && Netsubtype = = Telephonymanager.network_type_umts) {
If the 3G
} else {
Other network
}
else {//If no network

}

3. Custom touch Create right and left stroke detection

The code is as follows Copy Code
Define gesture Speed Recorder
Private Velocitytracker Velocitytracker;
Define the distance between gesture action two points
private static final int velocityx = 900;
private static final int velocityy = 800;

WebView postcontent = (webview) Findviewbyid (r.id.postcontent);

Postcontent.setontouchlistener (New Ontouchlistener () {
@Override
public boolean Ontouch (View arg0, motionevent arg1) {
TODO auto-generated Method Stub

Switch (arg1.getaction ()) {
Case Motionevent.action_down:
if (Velocitytracker = = null) {
Gets the speed at which gestures are sliding on the screen
Velocitytracker = Velocitytracker.obtain ();
Velocitytracker.addmovement (ARG1);
}
Break
Case Motionevent.action_move:
if (Velocitytracker!= null) {
Velocitytracker.addmovement (ARG1);
}
Break
Case Motionevent.action_cancel:
Case Motionevent.action_outside:
Break
Case MOTIONEVENT.ACTION_UP:

int Velocityx = 0, velocityy = 0;

if (Velocitytracker!= null) {
Velocitytracker.addmovement (ARG1);
Velocitytracker.computecurrentvelocity (1000); Calculate how many pixels to slide per second
Velocityx = (int) velocitytracker.getxvelocity (); Finally compute the retrieval X speed
Velocityy = (int) velocitytracker.getyvelocity (); Finally compute the search Y speed
}

Stroke from right to left
if (Velocityx <-velocityx && Velocityy < velocityy && velocityy >-velocityy) {
Perform the appropriate action
return true;
else if (Velocityx > Velocityx && velocityy < velocityy && velocityy >-velocityy) {//From left to right
Perform the appropriate action
return true;
} else {
To judge if it is not a stroke operation, return false using WebView's own touch handling
return false;
}

}
return false;
}
});

In the detection process found that the touch is not complete (such as too little stroke distance, or touch the screen), or directly to determine the operation is not a stroke, must return false, so that the components of their own touching processing to retain.

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.