私はApp開発の初心者です。準備が整ったソースコードを用意しています。 以下の2つのファイルを見つけてください。最初にログインしてホームページにリダイレクトしたいのですが、MainActivityをMainActivityに変更して、IntroActivityファイルのユーザーがログインウィンドウを取得してもログイン後MainActivityページにリダイレクトしないようにしてください。ちょうど私が変更する必要があるところでsuggesstしてください。アプリケーションのユーザーに最初にログインするように強制します。
コードは以下のとおりです。
ファイル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>
事前に感謝します。
好奇心の怪しさから、自分のコードの仕組みを理解していますか? – Anubis