Android 강제 오프라인 기능 구현 예시 코드

돌이켜보다
  • 지난번 에 연 재 된 두 가지 유형 을 썼 는데 한 가지 유형ActivityCollector.java은 모든 활동 을 관리 하 는 데 사용 되 었 다.하 나 는BaseActivity.java모든 활동 의 부류 이다.
  • layot 디 렉 터 리 에 놓 인 로그 인 인터페이스login.xml
  • 2.로그 인 페이지 의 이벤트
    다음은 로그 인 페이지 의 이 벤트 를 작성 합 니 다.계승BaseActivity.java
    
    package com.example.broadcastbestpractice;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class LoginActivity extends BaseActivity{
     private EditText accountEdit;
     
     private EditText passwordEdit;
     
     private Button login;
     
     @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.login);
     accountEdit = (EditText)findViewById(R.id.account);
     passwordEdit = (EditText)findViewById(R.id.password);
     login = (Button) findViewById(R.id.login);
     login.setOnClickListener( new OnClickListener() {
     @Override
     public void onClick(View v) {
     String account =accountEdit.getText().toString();
     String password = passwordEdit.getText().toString();
     //     admin,   12345,       
     if(account.contentEquals("admin") && password.equals("12345")) {
     Intent intent = new Intent(LoginActivity.this,MainActivity.class);
     startActivity(intent);
     finish();
     }else {
     Toast.makeText(LoginActivity.this,"account or password is invalid",Toast.LENGTH_SHORT).show();
     }
     } 
     }); 
     }
    }
  • 사용findViewById방법 으로 입력 상자 와 로그 인 단추 의 인 스 턴 스 를 각각 가 져 옵 니 다
  • 그 다음 에 클릭 사건 을 설정 합 니 다.먼저 계 정과 비밀번호 가 맞 는 지 판단 합 니 다.참,intent 인 스 턴 스 로 메 인 활동 에 들 어 가 는 것 입 니 다.틀 리 면 로그 인 페이지 에 다시 들 어가 서 제시 어 를 출력 합 니 다.
  • 이어서 메 인 화면 을 개조 해 보 세 요.물론 강제 오프라인 기능 을 더 하면 됩 니 다.다른 화려 한 것 은 필요 없습니다.
  • 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >
    
     <Button
     android:id="@+id/force_offline"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Send force offline broadcast" />
    
    </LinearLayout>
  • 아주 간단 합 니 다.바로 버튼 을 추가 한 것 입 니 다
  • 다음 에 주 활동 의 논 리 를 수정 합 니 다
  • 
    package com.example.broadcastbestpractice;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     Button forceOffline = (Button) findViewById(R.id.force_offline);
     forceOffline.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
     Intent intent = new Intent("com.example.broadcastbestpractice.FORCE_OFFLINE");
     sendBroadcast(intent);
     }
     });
     }
    }
  • 클릭 이벤트 에서 저 희 는com.example.broadcastbestpractice.FORCE_OFFLINE방송 을 보 내 서 프로그램 이 사용자 의 오프라인 을 강제 하 는 데 사 용 했 습 니 다.
  • 이 는 사용자 의 오프라인 을 강제 하 는 기능 을 설명 하 는 것 으로 수신 기 에 써 야 하 며 구체 적 인 특정한 활동 에 쓰 지 않 고'오프라인'방송 을 할 때 오프라인 작업 을 완성 할 수 있다.
  • 다음 에 라디오 수신 기 를 만 듭 니 다
  • 
    package com.example.broadcastbestpractice;
    
    import android.app.AlertDialog;
    import android.app.AlertDialog.Builder;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.view.WindowManager;
    import android.content.DialogInterface.OnClickListener;;
    
    public class ForceOfflineReceiver extends BroadcastReceiver{
     @Override
     public void onReceive(final Context context,Intent intent) {
     AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
     dialogBuilder.setTitle("Warning");
     dialogBuilder.setMessage("You are forced to be offline,Please try to login again.");
     dialogBuilder.setCancelable(false);
     //      
     dialogBuilder.setPositiveButton("OK", new OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog,int which) {
     ActivityCollector.finishAll();//       
     Intent intent = new Intent(context,LoginActivity.class);
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     context.startActivity(intent);//    LoginActivity
     }
     });
     AlertDialog alertDialog = dialogBuilder.create();
     //    AlertDialog   ,              
     alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
     alertDialog.show();
     }
    }

    25.1
    3.소스 코드:
    BroadcastBestPractice
    https://github.com/ruigege66/Android/tree/master/BroadcastBestPractice
    안 드 로 이 드 가 강제 오프라인 기능 을 실현 하 는 예제 코드 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 안 드 로 이 드 강제 오프라인 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!

    좋은 웹페이지 즐겨찾기