Android代码题


6个文件

  1. MainActivity.java
  2. ShowActivity.java
  3. activity_main.xml
  4. activity_show.xml
  5. RebootService.java
  6. AndroidManifest.xml

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.hello.myapplication;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.EditView;

public class MainActivity extends AppCompatActivity {
EditView mUsername;
EditView mPassword;
Button mRegister;
Intent intent;
Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsername = (EditView) findViewById(R.id.mUsername);
mPassword = (EditView) findViewById(R.id.mPassword);
mRegister = (Button)findViewById(R.id.mRegister);

mRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
intent = new Intent(MainActivity.this, ShowActivity.class);
bundle = new Bundle();
bundle.putString("username", mUsername.getText().toString());
bundle.putString("password", mPassword.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
});
}

@Override
protected void onDestroy(){
intent = new Intent(this, RebootService.class);
startService(intent);
super.onDestroy();
}
}

ShowActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.hello.myapplication;


import android.support.v7.app.AppCompatActivity;

import android.widget.TextView;

import android.content.Intent;
import android.os.Bundle;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ShowActivity extends AppCompatActivity {
TextView mShow;

JSONArray array;
JSONObject object;

String str;
Intent intent;
Bundle bundle;

FileInputStream fileIn;
FileOutputStream fileOut;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
mShow = (TextView) findViewById(R.id.mShow);

//读JSON
array = new JSONArray();
try{
fileIn = openFileInput("user.json");
byte[] buffer = new byte[fileIn.available()];
fileIn.read(buffer);
array = new JSONArray(new String(buffer));
fileIn.close();
}catch(Exception e){
e.printStackTrace();
}

intent = getIntent();
bundle = intent.getExtras();

//写JSON
try {
fileOut = openFileOutput("user.json", MODE_PRIVATE);
object = new JSONObject();
object.put("username", bundle.getString("username"));
object.put("password", bundle.getString("password"));
array.put(object);
fileOut.write(array.toString().getBytes());
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
//显示已注册的用户
try{
str = "所有用户信息\n\n";
for(int i=0; i<array.length(); i++){
object = array.getJSONObject(i);
str += "username: " + object.getString("username") + "\n";
str += "password: " + object.getString("password") + "\n\n";
}
mShow.setText(str);
}catch (Exception e){
e.printStackTrace();
}
}
}

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<EditText
android:id="@+id/mUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:hint="请输入用户名"/>

<EditText
android:id="@+id/mPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:hint="请输入密码"
android:textSize="20dp" />
<Button
android:id="@+id/mRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"/>
</LinearLayout>

activity_show.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ShowActivity">

<TextView
android:id="@+id/mShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

RebootService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.hello.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class RebootService extends Service {
Intent intentActivity;
public RebootService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
intentActivity = new Intent(this, MainActivity.class);
intentActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentActivity);
return super.onStartCommand(intent, flags, startId);
}
}

AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hello.myapplication">

<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">
<service
android:name=".RebootService"
android:enabled="true"
android:exported="true"></service>

<activity android:name=".ShowActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>