Skip to content

Commit

Permalink
Merge pull request #1934 from Seechai47/master
Browse files Browse the repository at this point in the history
#5 #978 第五次实验
  • Loading branch information
zengsn authored Jun 11, 2019
2 parents 45555e5 + 3d6b244 commit d5b7b67
Show file tree
Hide file tree
Showing 15 changed files with 761 additions and 0 deletions.
43 changes: 43 additions & 0 deletions students/soft1714080902333/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.hzuapps.androidlabs.soft1714080902333">

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

<!-- 存储权限-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- 摄像头权限-->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<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"
android:usesCleartextTraffic="true">
<activity android:name=".activity.StoreActivity" />
<activity android:name=".activity.NewsActivity" />
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package edu.hzuapps.androidlabs.soft1714080902333.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.acker.simplezxing.activity.CaptureActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import java.io.UnsupportedEncodingException;
import java.util.List;

import cz.msebera.android.httpclient.Header;
import edu.hzuapps.androidlabs.soft1714080902333.adapter.NewsAdapter;
import edu.hzuapps.androidlabs.soft1714080902333.bean.NewsInfo;
import edu.hzuapps.androidlabs.soft1714080902333.R;
import edu.hzuapps.androidlabs.soft1714080902333.utils.HttpUtils;

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

private static final int REQ_CODE_PERMISSION = 0x1111;

private ListView lv_news;
private List<NewsInfo> newsInfos;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

lv_news = (ListView) findViewById(R.id.lv_news);
fillData();
}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.nav_camera) { // 启动二维码扫描
// 动态申请权限
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[] {Manifest.permission.CAMERA}, REQ_CODE_PERMISSION);
} else {
startCaptureActivityForResult();
}

} else if (id == R.id.nav_gallery) { // 启动便签
Intent intent = new Intent();
intent.setClass(MainActivity.this, StoreActivity.class);
startActivity(intent);
MainActivity.this.finish();
} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.nav_manage) {

} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQ_CODE_PERMISSION: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 同意权限申请
startCaptureActivityForResult();
} else {
// 不同意权限申请
Toast.makeText(this, "相机权限为开启,无法使用二维码扫描功能", Toast.LENGTH_LONG).show();
}
}
break;
}
}

// 解析JSON文件
private void fillData() {
AsyncHttpClient client = new AsyncHttpClient();
// 获取JSON文件(存储在Tomcat上)
client.get("http://10.60.223.30:8080/NewsInfo.json", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
String json = new String(responseBody, "utf-8");
newsInfos = HttpUtils.getNewsInfo(json);
// 解析失败
if (newsInfos == null) {
Toast.makeText(MainActivity.this, "解析失败", Toast.LENGTH_LONG).show();
}
// 解析成功
else {
// 设置ListView内容
lv_news.setAdapter(new NewsAdapter(MainActivity.this, newsInfos));
// 设置点击事件
lv_news.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bundle bundle = new Bundle();
bundle.putString("uri", newsInfos.get(position).getUrl());
Intent intent = new Intent();
intent.setClass(MainActivity.this, NewsActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}

// 启动二维码扫描目标Activity
private void startCaptureActivityForResult() {
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
Bundle bundle = new Bundle();

bundle.putBoolean(CaptureActivity.KEY_NEED_BEEP, CaptureActivity.VALUE_BEEP);
bundle.putBoolean(CaptureActivity.KEY_NEED_VIBRATION, CaptureActivity.VALUE_VIBRATION);
bundle.putBoolean(CaptureActivity.KEY_NEED_EXPOSURE, CaptureActivity.VALUE_NO_EXPOSURE);
bundle.putByte(CaptureActivity.KEY_FLASHLIGHT_MODE, CaptureActivity.VALUE_FLASHLIGHT_OFF);
bundle.putByte(CaptureActivity.KEY_ORIENTATION_MODE, CaptureActivity.VALUE_ORIENTATION_AUTO);
bundle.putBoolean(CaptureActivity.KEY_SCAN_AREA_FULL_SCREEN, CaptureActivity.VALUE_SCAN_AREA_FULL_SCREEN);
bundle.putBoolean(CaptureActivity.KEY_NEED_SCAN_HINT_TEXT, CaptureActivity.VALUE_SCAN_HINT_TEXT);

intent.putExtra(CaptureActivity.EXTRA_SETTING_BUNDLE, bundle);
startActivityForResult(intent, CaptureActivity.REQ_CODE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package edu.hzuapps.androidlabs.soft1714080902333.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

import edu.hzuapps.androidlabs.soft1714080902333.R;

public class NewsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);

// 新页面接收数据
Bundle bundle = this.getIntent().getExtras();
// 接收name值
String name = bundle.getString("uri");

WebView webView = (WebView) findViewById(R.id.mWebView);

WebSettings webSettings = webView.getSettings();

// Js交互权限
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

webSettings.setDomStorageEnabled(true);

webView.loadUrl(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package edu.hzuapps.androidlabs.soft1714080902333.activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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

import edu.hzuapps.androidlabs.soft1714080902333.R;

public class StoreActivity extends AppCompatActivity {
private EditText ip_info;
private Button but_rd;
private Button but_wr;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);

// 获取Layout控件
ip_info = (EditText) findViewById(R.id.ip_info);
but_rd = (Button) findViewById(R.id.but_rd);
but_wr = (Button) findViewById(R.id.but_wr);

but_wr.setOnClickListener(new ButtonListener());
but_rd.setOnClickListener(new ButtonListener());
}

// Button点击事件
private class ButtonListener implements View.OnClickListener {
public void onClick(View view) {
switch (view.getId()) {
case R.id.but_wr:
String message_wr = "\n";
message_wr = ip_info.getText().toString().trim();
FileOutputStream fileOutputStream;
try {
fileOutputStream = openFileOutput("cache.txt", Context.MODE_APPEND);
fileOutputStream.write(message_wr.getBytes());
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(StoreActivity.this, "数据保存成功", Toast.LENGTH_SHORT)
.show();
break;
case R.id.but_rd:
String message_rd = "";
try {
FileInputStream fileInputStream = openFileInput("cache.txt");
byte[] buffer = new byte[fileInputStream.available()];
fileInputStream.read(buffer);
message_rd = new String(buffer);
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(StoreActivity.this, "保存的数据是:" + message_rd,
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
}
Loading

0 comments on commit d5b7b67

Please sign in to comment.