2 sinat 28626093 sinat_28626093 于 2015.06.28 11:17 提问

Android activity跳转与线程启动问题

package com.example.gteetcard;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

public static  PlayMisic Music;
public final static int result_code = 0;
public final static int result_code_animation = 1;
private Button delete;//删除内容按钮
private Button changebackground;//更改背景图片按钮
private Button animation;
private Button musicplayer;
private EditText EditText;//输入的文本框
private Button save;
private  LinearLayout lay1;
public static Handler myHandler;
public static gameview mGameView;
public static int flag;

int i;

@SuppressLint("HandlerLeak")
public void onActivityResult(int requestCode,int resultCode,Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==result_code)
    {
        int a = data.getIntExtra("background_data", R.drawable.picture5);
        lay1 .setBackgroundResource(a);
    }
    if(requestCode==result_code_animation)
    {
         int b=data.getIntExtra("data_animation", 1);
         mGameView.selt = b-1;
    }   
}
protected void onCreate(Bundle savedInstanceState) {

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


    lay1 =(LinearLayout)findViewById(R.id.Bac_Linearlayout);
    EditText = (EditText)findViewById(R.id.EditText);
    delete=(Button)findViewById(R.id.delete);
    musicplayer=(Button)findViewById(R.id.play_music);
    save=(Button)findViewById(R.id.save);
   mGameView=(gameview)findViewById(R.id.view);
   mGameView.gameviewInit();


   myHandler = new Handler() {  
        public void handleMessage(Message msg) {   
             if (msg.what==0x101) //根据收到的what类进行处理
             {   
                    mGameView.invalidate();  
             }   
            super.handleMessage(msg);   
        }   
    };
   class myThread implements Runnable {   
        public void run() {  
        while (!Thread.currentThread().isInterrupted()){    
                   Message message = myHandler.obtainMessage(); // 获取一个Message  
                   message.what = 0x101;//设置消息标识   
                   myHandler.sendMessage(message); // 发送消息 
                   for(i=0;i<10;i++)
                   {
                       mGameView.x[i] = 0+i*70+i%5;
                       mGameView.y[i] += 0+(int)(Math.random()*30*(i%2+1));
                       if(mGameView.y[i] > 900)mGameView.y[i] = 0;
                   }                      
                   try{   
                            Thread.sleep(80);    
                    } 
                   catch (InterruptedException e) {   
                                 Thread.currentThread().interrupt();   
                   } 
                   mGameView.postInvalidate();
              }   
         }   
    } 

    myThread mThread = new myThread();
    Thread thread = new Thread(mThread);
    thread.start();




    save.setOnClickListener(new OnClickListener(){

        public void onClick(View arg0) {
            EditText.setEnabled(false);
        }
    });


    //删除内容的事件监听
    delete.setOnClickListener(new OnClickListener(){

        public void onClick(View arg0) {
            EditText.setText("");   
            EditText.setEnabled(true);
        }
    });


    //更改照片的按钮
    changebackground=(Button)findViewById(R.id.changebackground);
    changebackground.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0){
            // TODO Auto-generated method stub
            Intent intent =new Intent();
            intent.setClass(MainActivity.this, ChangeBackgroundActivity.class);
            startActivityForResult(intent,result_code); 
                }
    });

    //浮动效果的按钮
    animation=(Button)findViewById(R.id.animation);
    animation.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainActivity.this,AnimationActivity.class); 
            startActivityForResult(intent,result_code_animation);
        }
    });

    //音乐播放事件监听
    musicplayer.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent=new Intent();
            intent.setClass(MainActivity.this, PlayMisic.class);
            startActivity(intent);
        }
    });

}   



public void onBackPressed() {//按下退出键时音乐停止
    // TODO Auto-generated method stub
    if(Music.mMediaPlayer.isPlaying())
    {
        Music.mMediaPlayer.stop();
        Music.mMediaPlayer.release();
    }

        finish();
}

}

我写的是一个写祝福的App,gameview是一个继承view的类,在主activity实现类似于QQ掉表情的浮动特效,我如果直接进入更换浮动特效页面,可以正常运行,但是我现在通过button跳转到别的activity再回来就没有浮动特效了,也没办法再更换浮动特效了,我觉得是我的线程写的有问题,上面是我的主activity的代码

求大神T T

1个回答

bdmh
bdmh   Ds   Rxr 2015.06.28 13:28

跳转后,你看看线程是不是已经停止了

Csdn user default icon
上传中...
上传图片
插入图片