安卓实现QQ强制下线----来自第一行代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓实现QQ强制下线----来自第一行代码相关的知识,希望对你有一定的参考价值。

首先需要一个能包含所有活动的类   到最后可以调用这个对象的方法 销毁所有活动

 

package com.example.broadcastbestpractice;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;

public class ActivityCollector {

private static List<Activity> activities = new ArrayList<Activity>();

public static void addActivity(Activity activity) {
activities.add(activity);
}

public static void removeActivity(Activity activity) {
activities.remove(activity);
}

public static void finishAll() {
for (Activity activity : activities) {
if (!activity.isFinishing()) {
activity.finish();
}
}
}

}

//需要所有类继承这个类 

package com.example.broadcastbestpractice;

import android.app.Activity;
import android.os.Bundle;

public class BaseActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}

@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}

}

 

 

登录活动 

package com.example.broadcastbestpractice;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

//登录活动
public class LoginActivity extends BaseActivity {


//用户名视图
private EditText accountEdit;

//密码视图
private EditText passwordEdit;

//登录按钮视图
private Button login;

//单选框视图
private CheckBox rememberPass;

@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);
rememberPass = (CheckBox) findViewById(R.id.remember_pass);
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();
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 invalid",
Toast.LENGTH_SHORT).show();
}
}
});
}

}

 

界面

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" >

<TableRow>

<TextView
android:layout_height="wrap_content"
android:text="用户名:" />

<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
</TableRow>

<TableRow>

<TextView
android:layout_height="wrap_content"
android:text="密码:" />

<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</TableRow>

<TableRow>

<CheckBox
android:id="@+id/remember_pass"
android:layout_height="wrap_content" />

<TextView
android:layout_height="wrap_content"
android:text="记住 密码" />
</TableRow>

<TableRow>

<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="登录" />
</TableRow>

</TableLayout>

 

另外一个活动来测试强制下线

package com.example.broadcastbestpractice;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
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 = (Button) findViewById(R.id.force_offline);
forceOffline.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("666");
Intent intent = new Intent(
"com.example.broadcastbestpractice.FORCE_OFFLINE");
sendBroadcast(intent);
}
});
}

}

 

界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.WindowManager;

public class ForceOfflineReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, Intent intent) {
System.out.println("555");
//弹出强制下线对话框
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 DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//点击确定后销毁所有活动
ActivityCollector.finishAll();
//启动登录活动
Intent intent = new Intent(context, LoginActivity.class);
System.out.println(context);
//因为是在接收器中启动活动需要添加标记位
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
AlertDialog alertDialog = dialogBuilder.create();
//
// //设置对话框的类型 保证在广播接收器中可以正常弹出
alertDialog.getWindow().setType(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
}

}

 

注册文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastbestpractice"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name=".LoginActivity"
android:label="mylogin" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" >
</activity>

<receiver android:name=".ForceOfflineReceiver" >
<intent-filter>
<action android:name="com.example.broadcastbestpractice.FORCE_OFFLINE" />
</intent-filter>
</receiver>

</application>

</manifest>



























































































































































































以上是关于安卓实现QQ强制下线----来自第一行代码的主要内容,如果未能解决你的问题,请参考以下文章

安卓模拟聊天界面---改编第一行代码

Android开发——《第一行代码》自学中

Android :安卓第一行代码学习笔记之 解析JSON格式数据

Android :第一行安卓代码学习笔记之 全局获取 Context

Android :安卓第一行代码学习笔记之 material design简单理解和使用

Android :安卓第一行代码学习笔记之 解析LifeCycle 的简单理解和使用