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

历览千载书,时时见遗烈。这篇文章主要讲述安卓实现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);
}

}
 
【安卓实现QQ强制下线----来自第一行代码】 
登录活动 
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>



























































































































































































    推荐阅读