如何使Android应用程序多线程

时间:2013-03-03 17:54:54

标签: java android multithreading

我已经创建了一个直到最近运行良好的应用程序...但是现在它崩溃时出现错误,声称“跳过63帧!应用程序可能在其主线程上做了太多工作。”

根据我收集的内容,我需要在应用程序中实现多线程,但我很好奇最简单,最快速,最有效的方法!

(我在下面提供了我的源代码)


AppActivity.java

package com.mkyong.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;

public class AppActivity extends Activity {

final Context context = this;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button = (Button) findViewById(R.id.button1);

// add button listener
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        context);

    // set title
    alertDialogBuilder.setTitle("Settings Menu");

    // set dialog message
    alertDialogBuilder
        .setMessage("Link or Delete?")
        .setCancelable(false)
        .setPositiveButton("Link",new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int id) {
        //start new activity

        Intent intentApp2Activity = new Intent(AppActivity.this, App2Activity.class);
        startActivity(intentApp2Activity);

        // if this button is clicked, close
        // current activity
        AppActivity.this.finish();
    }
  })
        .setNegativeButton("Delete",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, just close
                // the dialog box and do nothing
                dialog.cancel();
            }
        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }
});
}}

App2Activity.java

package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class App2Activity extends Activity {

Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
}

}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1" android:layout_width="fill_parent"     android:layout_height="fill_parent">
<TextView android:textAppearance="?android:textAppearanceLarge" android:id="@+id/textView1"     android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="NFC Linking     Manager" />
<Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Linksys Router (Home)" android:onClick="onPopupBtClick" />
<Button android:id="@+id/Button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Netgear Router (Office)" />
<Button android:id="@+id/Button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Cisco Router (Office)" />
</LinearLayout>

main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:id="@+id/linearLayout1"     android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:textAppearance="?android:textAppearanceLarge" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="User Settings:" />
<TextView android:textAppearance="?android:textAppearanceLarge" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<TextView android:textAppearance="?android:textAppearanceMedium" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Data Limit" />
<TextView android:textAppearance="?android:textAppearanceSmall" android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<SeekBar android:id="@+id/seekBar1" android:layout_width="fill_parent" android:layout_height="wrap_content" />
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
    <TextView android:gravity="left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="10MB" android:layout_weight="1.0" />
    <TextView android:gravity="right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Unlimited Data" android:layout_weight="1.0" />
</LinearLayout>
<TextView android:textAppearance="?android:textAppearanceSmall" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<TextView android:textAppearance="?android:textAppearanceMedium" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bandwidth Limit" />
<TextView android:textAppearance="?android:textAppearanceSmall" android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<SeekBar android:id="@+id/seekBar1" android:layout_width="fill_parent" android:layout_height="wrap_content" />
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
    <TextView android:gravity="left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="10kbs" android:layout_weight="1.0" />
    <TextView android:gravity="right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Unlimited Bandwidth" android:layout_weight="1.0" />
</LinearLayout>
<TextView android:textAppearance="?android:textAppearanceSmall" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:textAppearance="?android:textAppearanceMedium" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="WiFi Time Limit" />
<TextView android:textAppearance="?android:textAppearanceSmall" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50.0dip" android:layout_weight="1.0" />
<TextView android:textAppearance="?android:textAppearanceSmall" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Blocked Sites - [ex: www.xxx.com]" android:ems="10" />
</LinearLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mkyong.android"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="11" />

<application
    android:label="@string/app_name" >
    <activity android:name=".App2Activity" >
    </activity>
    <activity
        android:name=".AppActivity"
        android:label="@string/app_name" >
    </activity>
</application>

</manifest>

3 个答案:

答案 0 :(得分:3)

多线程不是一个简单的插件工具。您只是要求我们构建/设计您的应用程序。首先阅读 Java:实践中的并发。

答案 1 :(得分:1)

Android为可以在线程中运行的简单任务提供AsyncTask类

答案 2 :(得分:0)

简单的方法可以解决以下问题:

new Thread(new Runnable() {
@Override
public void run() {
     //Dosomething here
}).start();

然而,这是非常基本的,让你无法控制你的线程