android實現強制下線功能
阿新 • • 發佈:2018-11-17
強制下線是一種很常見的場面,比如我們的qq被別人登陸了,那麼你就會強制下線。
它的思路是什麼樣的呢。
其實也並不難,我們只需要在介面上彈出一個對話方塊,讓使用者無法進行其他操作,必須點選對話方塊的確定按鈕,然後返回登入介面。
但存在一個問題,我們被強制下線時,我們可能正在打遊戲,咔擦,完了,一個對話方塊彈出來,你遊戲就gg了。
強制下線需要關閉所有的活動,然後回到登入介面。
配置檔案mainifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sunshunli.broadcastbestpractice"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> </activity> <activity android:name=".LoginActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
mainactivity:
package com.example.sunshunli.broadcastbestpractice; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button forceoffline = findViewById(R.id.fore_offlinen); forceoffline.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.example.broadcastpractice.FORCE_OFFLINE"); sendBroadcast(intent); } }); } }
Baseactivivty
package com.example.sunshunli.broadcastbestpractice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; public class BaseActivity extends AppCompatActivity{ private ForceOfflineReceive receiver; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityCollector.addActivity(this); } @Override protected void onPostResume() { super.onPostResume(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("com.example.broadcastpractice.FORCE_OFFLINE"); receiver = new ForceOfflineReceive(); registerReceiver(receiver,intentFilter); } @Override protected void onPause() { super.onPause(); if(receiver!=null){ unregisterReceiver(receiver); receiver=null; } } protected void onDestory(){ super.onDestroy(); ActivityCollector.removeActivity(this); } class ForceOfflineReceive extends BroadcastReceiver{ @Override public void onReceive(final Context context, Intent intent) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Warning"); builder.setMessage("You are force to be offline ,please try to login again"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ActivityCollector.finshall(); Intent intent =new Intent(context,LoginActivity.class); context.startActivity(intent); } }); builder.show(); } } }
loginactivity:
package com.example.sunshunli.broadcastbestpractice;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends BaseActivity{
private EditText accoutEdit;
private EditText passwordEdit;
private Button login;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
accoutEdit=findViewById(R.id.account);
passwordEdit=findViewById(R.id.password);
login=findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String account = accoutEdit.getText().toString();
String password = passwordEdit.getText().toString();
if (account.equals("admin")&&password.equals("123456")){
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}else{
Toast.makeText(LoginActivity.this, "account or password is error", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activitycollector:
package com.example.sunshunli.broadcastbestpractice;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
public class ActivityCollector {
public static List<Activity> activities = new ArrayList<>();
public static void addActivity(Activity activity){
activities.add(activity);
}
public static void removeActivity(Activity activity){
activities.remove(activity);
}
public static void finshall(){
for (Activity activity :activities){
if (!activity.isFinishing()){
activity.finish();
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
tools:context=".LoginActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="Account:"
/>
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/account"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="Password:"
/>
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/password"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</LinearLayout>
<Button
android:id="@+id/login"
android:text="login"
android:layout_width="match_parent"
android:layout_height="60dp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<Button
android:id="@+id/fore_offlinen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="send force offioce broadcast"/>
</LinearLayout>