活动堆叠在现有活动

时间:2016-09-23 05:31:46

标签: java android

我的Android应用程序出了点问题。这是一个使用加速度计确定手的稳定性的应用程序。我有两个活动,Vert.java和Calcu.java。在Vert.java中有一个包含25个png文件的动画功能。它还具有检索x,y和z轴的加速度计值的功能。组合值将传输到Calcu.java,其中使用if循环给出特定值范围的分数。但是,运行"计算"活动再次出现。我是android的新手。请帮忙。

这些是我的代码..

Vert.java

import java.util.Timer;
import java.util.TimerTask;



import android.app.Activity;
import android.content.Intent;

import android.graphics.drawable.AnimationDrawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;

import android.widget.ImageView;

public class Vert extends Activity implements SensorEventListener{
    Sensor accelerometer1;
    SensorManager sm1;
    float x1=0,y1=0,z1=0,tx1=0,ty1=0,tz1=0,oldx1=0,oldy1=0,oldz1=0,totalx1=0,totaly1=0,totalz1=0;
    Intent m1;  
    int z;
    ImageView img;
    Timer timer;
    Handler handler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        overridePendingTransition(R.anim.slide_in,R.anim.slide_out);
        setContentView(R.layout.vert);
        sm1=(SensorManager)getSystemService(SENSOR_SERVICE);
        accelerometer1=sm1.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm1.registerListener(this, accelerometer1,SensorManager.SENSOR_DELAY_NORMAL);
        img=(ImageView)findViewById(R.id.ivAnim);
        AnimationDrawable animation=new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(R.drawable.a),200);
        animation.addFrame(getResources().getDrawable(R.drawable.b),200);
        animation.addFrame(getResources().getDrawable(R.drawable.c),200);
        animation.addFrame(getResources().getDrawable(R.drawable.d),200);
        animation.addFrame(getResources().getDrawable(R.drawable.e),200);
        animation.addFrame(getResources().getDrawable(R.drawable.f),200);
        animation.addFrame(getResources().getDrawable(R.drawable.g),200);
        animation.addFrame(getResources().getDrawable(R.drawable.h),200);
        animation.addFrame(getResources().getDrawable(R.drawable.i),200);
        animation.addFrame(getResources().getDrawable(R.drawable.j),200);
        animation.addFrame(getResources().getDrawable(R.drawable.k),200);
        animation.addFrame(getResources().getDrawable(R.drawable.l),200);
        animation.addFrame(getResources().getDrawable(R.drawable.m),200);
        animation.addFrame(getResources().getDrawable(R.drawable.n),200);
        animation.addFrame(getResources().getDrawable(R.drawable.o),200);
        animation.addFrame(getResources().getDrawable(R.drawable.p),200);
        animation.addFrame(getResources().getDrawable(R.drawable.q),200);
        animation.addFrame(getResources().getDrawable(R.drawable.r),200);
        animation.addFrame(getResources().getDrawable(R.drawable.s),200);
        animation.addFrame(getResources().getDrawable(R.drawable.t),200);
        animation.addFrame(getResources().getDrawable(R.drawable.u),200);
        animation.addFrame(getResources().getDrawable(R.drawable.v),200);
        animation.addFrame(getResources().getDrawable(R.drawable.w),200);
        animation.addFrame(getResources().getDrawable(R.drawable.x),200);
        animation.addFrame(getResources().getDrawable(R.drawable.y),200);
    animation.setOneShot(false);
    img.setImageDrawable(animation);
    animation.start();
    }
    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        x1=event.values[0];
        y1=event.values[1];
        z1=event.values[2];

        timer=new Timer();
        timer.scheduleAtFixedRate(new TimerTask(){

            @Override
            public void run() {
                // TODO Auto-generated method stub

                oldx1=x1;
                oldy1=y1;
                oldz1=z1;

                            }

        }, 200, 200);

        String str=String.valueOf(tx1);
        m1=new Intent(Vert.this,Calcu.class);
        m1.putExtra("score", str);


        if(oldx1>x1){
            totalx1=oldx1-x1;
        }
        else if(x1>oldx1){
            totalx1=x1-oldx1;
        }
        if(oldy1>y1){
            totaly1=oldy1-y1;
        }
        else if(y1>oldy1){
            totaly1=y1-oldy1;
        }
        if(oldz1>z1){
            totalz1=oldz1-z1;
        }
        else if(z1>oldz1){
            totalz1=z1-oldz1;
        }
        tx1=tx1+(totalx1+totaly1+totalz1);
         handler=new Handler();
         handler.postDelayed(new Runnable(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
                startActivity(Vert.this.m1);
                m1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                finish();
        }

         },5000);
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        this.sm1.unregisterListener(this);
        timer.cancel();


    }

}

Vert.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/vertpageplain"
    android:padding="20dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvVertHead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="30dp"
         android:textColor="#138808"
        android:text="KEEP YOUR HANDS STEADY TILL THE BOTTLE GETS EMPTY..." />

    <ImageView
        android:id="@+id/ivAnim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:src="@drawable/a" />

</LinearLayout>

Calcu.java

import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

public class Calcu extends Activity{
    float krip1;
    double score1;
    String sScore;
    TextView finalScore1;
    ImageButton bB1,bS1,bE1;
    Bitmap b;
    Intent intent,chooser;
    Uri imageUri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        overridePendingTransition(R.anim.slide_in,R.anim.slide_out);
        setContentView(R.layout.result);
        String pky1 = getIntent().getStringExtra("score");
        krip1=Float.valueOf(pky1);
        finalScore1=(TextView)findViewById(R.id.tvScore1);
        bB1=(ImageButton)findViewById(R.id.bMain1);
        bS1=(ImageButton)findViewById(R.id.bShare1);
        bE1=(ImageButton)findViewById(R.id.bExit1);
        bB1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent n=new Intent(Calcu.this,Play.class);
                n.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                n.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(n);

            }
        });
        bS1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(score1==10){
                       b=BitmapFactory.decodeResource(getResources(), R.drawable.scorea);

                   }
                   else if(score1==9.5){
                      b=BitmapFactory.decodeResource(getResources(), R.drawable.scoreb);
                   }
                   else if(score1==9){
                          b=BitmapFactory.decodeResource(getResources(), R.drawable.scorec);
                       }
                   else if(score1==8.5){
                          b=BitmapFactory.decodeResource(getResources(), R.drawable.scored);
                       }
                   else if(score1==8){
                          b=BitmapFactory.decodeResource(getResources(), R.drawable.scoree);
                       }
                   else if(score1==7.5){
                          b=BitmapFactory.decodeResource(getResources(), R.drawable.scoref);
                       }
                   else if(score1==7){
                          b=BitmapFactory.decodeResource(getResources(), R.drawable.scoreg);
                       }
                   else if(score1==6.5){
                              b=BitmapFactory.decodeResource(getResources(), R.drawable.scoreg);
                           }
                   else if(score1==6){
                              b=BitmapFactory.decodeResource(getResources(), R.drawable.scorei);
                           }
                   else if(score1==5.5){
                              b=BitmapFactory.decodeResource(getResources(), R.drawable.scorej);
                           }
                   else if(score1==5){
                              b=BitmapFactory.decodeResource(getResources(), R.drawable.scorek);
                           }
                   else if(score1==4.5){
                              b=BitmapFactory.decodeResource(getResources(), R.drawable.scorel);
                           }
                   else if(score1==4){
                                  b=BitmapFactory.decodeResource(getResources(), R.drawable.scorem);
                               }
                   else if(score1==3.5){
                                  b=BitmapFactory.decodeResource(getResources(), R.drawable.scoren);
                               }
                  else if(score1==3){
                                  b=BitmapFactory.decodeResource(getResources(), R.drawable.scoreo);
                               }
                  else if(score1==2.5){
                                  b=BitmapFactory.decodeResource(getResources(), R.drawable.scorep);
                               }
                  else if(score1==2){
                                  b=BitmapFactory.decodeResource(getResources(), R.drawable.scoreq);
                               }
                  else if(score1==1.5){
                                      b=BitmapFactory.decodeResource(getResources(), R.drawable.scorer);
                                   }
                  else if(score1==1){
                                      b=BitmapFactory.decodeResource(getResources(), R.drawable.scores);
                                   }
                  else if(score1==0.5){
                                      b=BitmapFactory.decodeResource(getResources(), R.drawable.scoret);
                                   }
                  else if(score1==0){
                      b=BitmapFactory.decodeResource(getResources(), R.drawable.scoreu);
                   }    
                intent=new Intent(Intent.ACTION_SEND);
                   intent.setType("image/png");
                   ByteArrayOutputStream bytes=new ByteArrayOutputStream();
                   b.compress(Bitmap.CompressFormat.PNG, 100, bytes);
                   String path=MediaStore.Images.Media.insertImage(getContentResolver(), b, "Title", null);
                   imageUri=Uri.parse(path);
                   intent.putExtra(Intent.EXTRA_TITLE, "SOBER SCORE CHALLENGE");
                   intent.putExtra(Intent.EXTRA_SUBJECT, "Sober score : "+score1+"What's your's?");
                   intent.putExtra(Intent.EXTRA_STREAM, imageUri);
                   intent.putExtra(Intent.EXTRA_TEXT, "This is beta version. The final version will soon be available in play store.");
                   chooser=Intent.createChooser(intent, "Share your Sober Score...");
                   startActivity(chooser);
            }
        });
        bE1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent exit=new Intent(getApplicationContext(),MainActivity.class);
                exit.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                exit.putExtra("EXIT", true);
                startActivity(exit);
            }
        });
        if(krip1<90){
            score1=10;

        }
        else if(90<krip1 && krip1<100){
            score1=9.5;

        }
        else if(100<krip1 && krip1<110){
            score1=9;

        }
        else if(110<krip1 && krip1<135){
            score1=8.5;

        }
        else if(135<krip1 && krip1<150){
            score1=8;

        }
        else if(150<krip1 && krip1<160){
            score1=7.5;

        }
        else if(160<krip1 && krip1<175){
            score1=7;

        }
        else if(175<krip1 && krip1<185){
            score1=6.5;

        }
        else if(185<krip1 && krip1<195){
            score1=6;

        }
        else if(195<krip1 && krip1<210){
            score1=5.5;

        }
        else if(210<krip1 && krip1<215){
            score1=5;

        }
        else if(215<krip1 && krip1<225){
            score1=4.5;

        }
        else if(225<krip1 && krip1<245){
            score1=4;

        }
        else if(245<krip1 && krip1<265){
            score1=3.5;

        }
        else if(265<krip1 && krip1<285){
            score1=3;

        }
        else if(285<krip1 && krip1<305){
            score1=2.5;

        }
        else if(305<krip1 && krip1<335){
            score1=2;

        }
        else if(335<krip1 && krip1<365){
            score1=1.5;

        }
        else if(365<krip1 && krip1<395){
            score1=1;

        }
        else if(395<krip1 && krip1<425){
            score1=0.5;

        }

        else if(krip1>425){
            score1=0;


        }

        finalScore1.setText(""+score1+"");
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        Intent back=new Intent(Calcu.this,MainActivity.class);
        back.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        back.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(back);
        Calcu.this.finish();
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        finish();
    }


}

为result.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/resultpageplain"
    android:orientation="vertical"
     >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:src="@drawable/sobercent" />

    <TextView
        android:id="@+id/tvScore1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="70dp"
         android:textColor="#138808"
        android:text="X" />
<LinearLayout
     android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <ImageButton
        android:id="@+id/bMain1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="0dp"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:src="@drawable/bplayagain" />

    <ImageButton
        android:id="@+id/bShare1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:padding="0dp"
         android:layout_marginTop="30dp"
         android:layout_marginLeft="50dp"
        android:src="@drawable/bshare" />

</LinearLayout>

<ImageButton
    android:id="@+id/bExit1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="0dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:src="@drawable/bexit" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

替换下面的代码片段,它应该工作

      @Override
        public void run() {
            // TODO Auto-generated method stub
            startActivity(Vert.this.m1);
            m1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            finish();
    }