English 中文(简体)
可能在甲状腺中泄露记忆。 如何使用错误的清理方法,或缺少一些东西
原标题:Possible memory leak in android. Might be using the wrong cleanup method, or missing something
  • 时间:2011-01-25 06:19:31
  •  标签:
  • android

我有记忆泄露。 这里的法典

package fourguys.testing.IntentTest;

import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.media.MediaPlayer; import android.media.AudioManager; import android.content.Context;

public class CanvasDrawingActivity extends Activity {

    private static final int FIRE = 0;
    private int initVolume = 0;
    private Handler handler;
    private MyCanvas v;
    private MediaPlayer mp;
    private AudioManager am;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);

        // this method gets the current volume setting for music
        initVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);


        am.setStreamVolume(AudioManager.STREAM_MUSIC,100,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

        mp = MediaPlayer.create(this, R.raw.test);

        makeHandler();
        v =new MyCanvas(this);
        new Thread(new Runnable(){
            @Override
            public void run() {
                while(true)
                handler.sendEmptyMessage(FIRE);
        }}).start();
        setContentView(v);
        mp.setLooping(true);
        mp.start();
    }
    private void makeHandler()
    {
        handler  = new Handler(){

            @Override
            public void handleMessage(Message msg) {
                switch(msg.what)
                {
                    case FIRE:
                    {
                        v.invalidate();
                        break;
                    }
                }
            }

        };
    }
    protected void onPause() {
        super.onPause();
        mp.stop();
    }
    protected void onFinish() {
        mp.stop();
    }

}

为此:

package fourguys.testing.IntentTest;

import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.view.WindowManager;

public class IntentTest extends Activity { /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        //reciever intentReceiver = new reciever();

        // IntentFilter intentFilter = new IntentFilter("com.app.REC");

        //registerReceiver(intentReceiver, intentFilter);
        Button b = (Button)this.findViewById(R.id.endButton);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(IntentTest.this,CanvasDrawingActivity.class);
                startActivity(i);

            }
        });
    }
    // the onPause method get called when the app is either being hidden or being closed so this the place where we would want to clean anything up like stoping the media player.
    @Override
    protected void onPause()
    {
        super.onPause();
    }
}

我拿着 app子,在离去后就获得了胜利。 它打开了手脚,使电池能够运行热。 我需要从身体上把电池 re回。 为什么会这样? 它热情地领导着支持者。 我是否应该改用Finish,或者我是否不清洗东西,我是谁失踪?

问题回答

这是你法典的这一部分:

 new Thread(new Runnable(){
            @Override
            public void run() {
                while(true)
                handler.sendEmptyMessage(FIRE);
        }}).start();

你在这里做三件明显的事情是错误的。 (1) 你们不要在<条码>上杀害和(或)在条码上pa弃。 2) You re not calls setDaemon(true);这将导致这一过程在进行之中继续,而不是死去。 3) 您重新使用热门,即:您不再使用<>条形码>,而代号为代号为<>。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签