App用户强制首先登录

时间:2017-07-21 06:35:35

标签: java android

我是App开发的初学者,我已经准备好了源代码。     请在下面找到两个文件,我想强制用户先登录然后重定向到主页,如果我在IntroActivity文件中将MainActivity更改为LoginActivity,则用户获取登录窗口但登录后不会重定向到MainActivity页面。请提出我需要改变的地方。

代码是:

文件1:IntroActivity.java

package com.ristana.bullish.ui.activity;

import android.content.Intent;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ristana.bullish.R;
import com.ristana.bullish.entity.ApiResponse;
import com.ristana.bullish.manager.PrefManager;

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

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import com.ristana.bullish.api.apiClient;
import com.ristana.bullish.api.apiRest;

public class IntroActivity extends AppCompatActivity {
    private ProgressBar intro_progress;
    private RelativeLayout activity_intro;
    private PrefManager prf;
    private TextView text_view_app_version;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        prf= new PrefManager(getApplicationContext());
        initView();
        Timer myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                // If you want to modify a view in your Activity
                IntroActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (prf.getString("LOGGED").contains("TRUE")){
                            checkAccount();
                        }else{
                            Intent intent = new Intent(IntroActivity.this,MainActivity.class);
                            startActivity(intent);
                        }
                    }
                });
            }
        }, 5000);
    }
    private  void initView(){
        setContentView(R.layout.activity_intro);
        this.intro_progress=(ProgressBar) findViewById(R.id.intro_progress);
        this.activity_intro=(RelativeLayout) findViewById(R.id.activity_intro);
        intro_progress.setVisibility(View.VISIBLE);
        this.text_view_app_version=(TextView) findViewById(R.id.text_view_app_version);
        try {
            PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(),0);
            String version = pInfo.versionName;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
    private void checkAccount() {

        intro_progress.setVisibility(View.VISIBLE);
        Retrofit retrofit = apiClient.getClient();
        apiRest service = retrofit.create(apiRest.class);
        Call<ApiResponse> call = service.check(prf.getString("ID_USER"),prf.getString("TOKEN_USER"));
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {

                if (response.isSuccessful()){
                    if (response.body().getCode()==200){
                        Intent intent = new Intent(IntroActivity.this,MainActivity.class);
                        startActivity(intent);
                    }else if(response.body().getCode()==500){
                        logout();
                        Intent intent = new Intent(IntroActivity.this,MainActivity.class);
                        startActivity(intent);
                    }else {
                        Intent intent = new Intent(IntroActivity.this,MainActivity.class);
                        startActivity(intent);
                    }
                }else{
                    Intent intent = new Intent(IntroActivity.this,MainActivity.class);
                    startActivity(intent);
                }

            }
            @Override
            public void onFailure(Call<ApiResponse> call, Throwable t) {
                Intent intent = new Intent(IntroActivity.this,MainActivity.class);
                startActivity(intent);
                intro_progress.setVisibility(View.INVISIBLE);

            }
        });

    }
    public      void logout(){
        PrefManager prf= new PrefManager(getApplicationContext());
        prf.remove("ID_USER");
        prf.remove("SALT_USER");
        prf.remove("TOKEN_USER");
        prf.remove("NAME_USER");
        prf.remove("TYPE_USER");
        prf.remove("USERNAME_USER");
        prf.remove("URL_USER");
        prf.remove("LOGGED");
        Toast.makeText(getApplicationContext(),getString(R.string.message_logout_desibaled),Toast.LENGTH_LONG).show();
    }
    @Override
    public  void onPause(){
        super.onPause();
        finish();
    }
}

文件2:activity_intro.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_intro"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.ristana.bullish.ui.activity.IntroActivity">
    <ProgressBar
        android:progressDrawable="@drawable/progress_yellow"
        android:indeterminateDrawable="@drawable/progress_yellow"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:id="@+id/intro_progress" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/logo_app"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_gravity="center"
            android:gravity="center"
            android:padding="5dp"
            android:textSize="20dp"
            android:textStyle="bold"
            android:layout_centerInParent="true"
            android:textColor="@color/primary_text"
            android:textAlignment="center"
            android:text=" "
            android:id="@+id/text_view_app_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_gravity="center"
            android:gravity="center"
            android:padding="8dp"
            android:textSize="18dp"
            android:layout_centerInParent="true"
            android:textColor="@color/primary_text"
            android:textAlignment="center"
            android:text=" "
            android:id="@+id/text_view_app_version"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <ImageView
        android:layout_width="130dp"
        android:layout_height="130dp"
        app:srcCompat="@drawable/logo"
        android:id="@+id/logo_app"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

提前致谢。

2 个答案:

答案 0 :(得分:0)

只需查看改装调用返回的响应代码是什么。 最好的方法是调试代码。成功响应/失败后,将编写代码。但仍需要知道代码是否到达那里。如果没有到达那里,则不会执行这些步骤。 希望有所帮助。

答案 1 :(得分:0)

正如评论中所述,我想你根本不知道这段代码是如何工作的,所以我会尽力为你简化。

首先,您正在使用改造,这是一个很好的工具,用于获取和发布数据到服务器没有任何麻烦(谷歌,你会发现更多)。然后,您使用PrefManager用于在应用程序存储中存储一些键/值对,以便您可以在需要时对其进行检索。 (What are the PreferenceManager and SharedPreference classes used for in Android?这里有一个可以清除的链接。

对于您询问的部分,您正在调用响应的改造,如果它成功响应(意味着登录数据已发送到服务器,经过测试,并且没问题),那么您就是创建new Intent告诉使用IntoActivity.this轻扫MainActivity.class(您的当前活动),然后使用startActivity(intent)将您重定向到MainActivity。你不能只是改变&#34;使用LoginActivity的MainActivty并等待重定向到Main。您可以将意图重定向到LoginActivity,然后再将其重定向到MainActivity(因为我不知道您在这里尝试做什么)。不要忘记,任何新活动都应该在清单中说明。

我会建议您在尝试使用此类代码之前阅读更多有关Android及其基础知识的内容,因为您将丢失。

祝你好运。