亲宝软件园·资讯

展开

android 强制下线 Android实现强制下线功能的代码实例

心悦君兮君不知-睿 人气:0

一、回顾

二、登录页面的活动

接下来写一个登录页面的活动,继承自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();
 }
 } 
 }); 
 }
}
<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);
 }
 });
 }
}
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

三、源码:

BroadcastBestPractice
https://github.com/ruigege66/Android/tree/master/BroadcastBestPractice

加载全部内容

相关教程
猜你喜欢
用户评论