实验结果:(未成功)
代码:
mainactivity.java
package com.example.password;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
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) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.example.broadcastbestpractice.FORCE_OFFLINE");
sendBroadcast(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
LoginActivity.java
package com.example.password;
import android.app.Activity;
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 Activity
{
private SharedPreferences pref;
private SharedPreferences.Editor editor;
private EditText accountEdit;
private EditText passwordEdit;
private Button login;
private CheckBox rememberPass;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.longin);
pref = PreferenceManager.getDefaultSharedPreferences(this);
accountEdit = (EditText) findViewById(R.id.account);
passwordEdit = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
rememberPass = (CheckBox) findViewById(R.id.remember_pass);
boolean isRemember = pref.getBoolean("remember_password", false);
if(isRemember){
//
String account = pref.getString("account", "");
String password = pref.getString("password", "");
accountEdit.setText(account);
passwordEdit.setText(password);
rememberPass.setChecked(true);
}
login.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
String account = accountEdit.getText().toString();
String password = passwordEdit.getText().toString();
if(account.equals("admin") && password.equals("123456")){
editor = pref.edit();
if(rememberPass.isChecked()){
editor.putBoolean("remember_pasword", true);
editor.putString("account", account);
editor.putString("password", password);
}else{
editor.clear();
}
editor.commit();
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();
}
}
});
}
}
main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/force_offline"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Send force offline broadcast"/>
</RelativeLayout>
login.xnl
<?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="Account:"/>
<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="Input our account"/>
</TableRow>
<TableRow >
<TextView
android:layout_height="wrap_content"
android:text="Password:"/>
<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:hint="Input our account"/>
</TableRow>
<TableRow >
<CheckBox
android:id="@+id/remember_pass"
android:layout_height="wrap_content"/>
<TextView
android:layout_height="wrap_content"
android:text="Remember password"/>
</TableRow>
<TableRow >
<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="Login"/>
</TableRow>
</TableLayout>
activityCollector.java
package com.example.password;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
public class ActivityCollector {
public 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();
}
}
}
}
BaseActivity.java
package com.example.password;
import android.app.Activity;
import android.os.Bundle;
public class BaseAcitivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}
public void onDestory(){
super.onDestroy();
ActivityCollector.removeActivity(this);
}
}
ForceOfflineReceiver.java
package com.example.password;
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
ActivityCollector.finishAll();
Intent intent = new Intent(context,LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
AlertDialog alertdialog = dialogBuilder.create();
alertdialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertdialog.show();
}
}
AndroidManifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.password"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<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="com.example.password.MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.password.LoginActivity"
android:label="登录界面">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.password.ForceOfflineReceiver">
<intent-filter>
<action android:name="com.example.broadcastbestpractice.FORCE_OFFLINE"/>
</intent-filter>
</receiver>
</application>
</manifest>
出现的问题: