Android Dialog Title 非显现
protected void dia() {
NDaig nDaig = new NDaig(this);
nDaig.requestWindowFeature(Window.FEATURE_NO_TITLE);
nDaig.show();
}
2017年1月23日月曜日
2017年1月16日月曜日
Java 回调
Java 回调
https://paiza.io/projects/3KFF7-6l4pA4pvijDyvPiw
public class CB2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
CBB2 cb = new CBB2();
cb.tt(new CBB2.CallBack() {
@Override
public void start() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName());
}
});
}
}
class CBB2{
public interface CallBack{
public void start();
}
public void tt(CallBack cb){
t1 tt1 = new t1(cb);
tt1.start();
}
public class t1 extends Thread{
private CallBack cb;
public t1(CallBack cb) {
this.cb = cb;
}
public void run(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cb.start();
}
}
}
https://paiza.io/projects/3KFF7-6l4pA4pvijDyvPiw
public class CB2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
CBB2 cb = new CBB2();
cb.tt(new CBB2.CallBack() {
@Override
public void start() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName());
}
});
}
}
class CBB2{
public interface CallBack{
public void start();
}
public void tt(CallBack cb){
t1 tt1 = new t1(cb);
tt1.start();
}
public class t1 extends Thread{
private CallBack cb;
public t1(CallBack cb) {
this.cb = cb;
}
public void run(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cb.start();
}
}
}
2017年1月12日木曜日
Java 线程同步。
Java 线程同步。
要锁定都一个资源。
public class T1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
TX re = new TX();
T2 t2 = new T2(re);
Thread t = new Thread(t2);
t.start();
T3 t3 = new T3(re);
Thread tt = new Thread(t3);
tt.start();
}
}
class TX {
public String name;
public String sex;
}
class T2 implements Runnable {
private TX r;
public T2(TX r) {
this.r = r;
}
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (true) {
synchronized (r) {
if (i == 0) {
r.name = "AAAA";
r.sex = "FFFFF";
} else {
r.name = "BBB";
r.sex = "mmmmmmmmmmmmmmmmmmmm";
}
}
i = (i + 1) % 2;
}
}
}
class T3 implements Runnable {
private TX r;
public T3(TX r) {
this.r = r;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
synchronized (r) {
System.out.println(r.name + "---------" + r.sex);
}
}
}
}
要锁定都一个资源。
public class T1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
TX re = new TX();
T2 t2 = new T2(re);
Thread t = new Thread(t2);
t.start();
T3 t3 = new T3(re);
Thread tt = new Thread(t3);
tt.start();
}
}
class TX {
public String name;
public String sex;
}
class T2 implements Runnable {
private TX r;
public T2(TX r) {
this.r = r;
}
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (true) {
synchronized (r) {
if (i == 0) {
r.name = "AAAA";
r.sex = "FFFFF";
} else {
r.name = "BBB";
r.sex = "mmmmmmmmmmmmmmmmmmmm";
}
}
i = (i + 1) % 2;
}
}
}
class T3 implements Runnable {
private TX r;
public T3(TX r) {
this.r = r;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
synchronized (r) {
System.out.println(r.name + "---------" + r.sex);
}
}
}
}
2016年12月25日日曜日
Android 外部存储
Android 外部存储
https://developer.android.com/guide/topics/data/data-storage.html
protected void saveto() {
// I/System.out: this.getDatabasePath("test")
// I/System.out: /data/user/0/com.example.e560.m1224a/databases/test
// I/System.out: /data/user/0/com.example.e560.m1224a/databases/test
// I/System.out: /data/user/0/com.example.e560.m1224a/databases/test
File f1 = this.getDatabasePath("test");
System.out.println("this.getDatabasePath(\"test\")");
System.out.println(f1.getAbsolutePath());
System.out.println(f1.getPath());
System.out.println(f1.toString());
// I/System.out: /storage/emulated/0/Android/data/com.example.e560.m1224a/files
// I/System.out: /storage/emulated/0/Android/data/com.example.e560.m1224a/files
// I/System.out: /storage/emulated/0/Android/data/com.example.e560.m1224a/files
File f2 = this.getExternalFilesDir(null);
System.out.println("this.getExternalFilesDir(null)");
System.out.println(f2.getAbsolutePath());
System.out.println(f2.getPath());
System.out.println(f2.toString());
// I/System.out: /data
// I/System.out: /data
// I/System.out: /data
File f3 = Environment.getDataDirectory();
System.out.println("Environment.getDataDirectory()");
System.out.println(f3.getAbsolutePath());
System.out.println(f3.getPath());
System.out.println(f3.toString());
// I/System.out: /storage/emulated/0
// I/System.out: /storage/emulated/0
// I/System.out: /storage/emulated/0
File f4 = Environment.getExternalStorageDirectory();
System.out.println("Environment.getExternalStorageDirectory()");
System.out.println(f4.getAbsolutePath());
System.out.println(f4.getPath());
System.out.println(f4.toString());
}
https://developer.android.com/guide/topics/data/data-storage.html
protected void saveto() {
// I/System.out: this.getDatabasePath("test")
// I/System.out: /data/user/0/com.example.e560.m1224a/databases/test
// I/System.out: /data/user/0/com.example.e560.m1224a/databases/test
// I/System.out: /data/user/0/com.example.e560.m1224a/databases/test
File f1 = this.getDatabasePath("test");
System.out.println("this.getDatabasePath(\"test\")");
System.out.println(f1.getAbsolutePath());
System.out.println(f1.getPath());
System.out.println(f1.toString());
// I/System.out: /storage/emulated/0/Android/data/com.example.e560.m1224a/files
// I/System.out: /storage/emulated/0/Android/data/com.example.e560.m1224a/files
// I/System.out: /storage/emulated/0/Android/data/com.example.e560.m1224a/files
File f2 = this.getExternalFilesDir(null);
System.out.println("this.getExternalFilesDir(null)");
System.out.println(f2.getAbsolutePath());
System.out.println(f2.getPath());
System.out.println(f2.toString());
// I/System.out: /data
// I/System.out: /data
// I/System.out: /data
File f3 = Environment.getDataDirectory();
System.out.println("Environment.getDataDirectory()");
System.out.println(f3.getAbsolutePath());
System.out.println(f3.getPath());
System.out.println(f3.toString());
// I/System.out: /storage/emulated/0
// I/System.out: /storage/emulated/0
// I/System.out: /storage/emulated/0
File f4 = Environment.getExternalStorageDirectory();
System.out.println("Environment.getExternalStorageDirectory()");
System.out.println(f4.getAbsolutePath());
System.out.println(f4.getPath());
System.out.println(f4.toString());
}
2016年12月24日土曜日
Android IntentService 类
Android IntentService 类
https://developer.android.com/guide/components/services.html
Develop > API Guides > 应用组件
//////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.e560.m1217a">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyServer"></service>
</application>
</manifest>
//////////////////////////////////////////////////////////////////////
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.LinearLayout;
public class MainActivity extends AppCompatActivity {
private int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
Button bt = new Button(this);
bt.setText("SaveFiletoAndroidFile");
linearLayout.addView(bt);
setContentView(linearLayout);
i = 0;
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i++;
SavetoServer(i);
}
});
}
protected void SavetoServer(int i) {
String[] imgURLs = new String[]{
"https://farm4.staticflickr.com/3239/2897452239_d2cf730467.jpg",
"https:\\/\\/farm4.staticflickr.com\\/3050\\/2897460805_781ed40c84.jpg",
"https:\\/\\/farm4.staticflickr.com\\/3553\\/3619665782_e9766aefe5.jpg",
"https:\\/\\/farm4.staticflickr.com\\/3082\\/2716691340_2ea1c206aa.jpg",
"https:\\/\\/farm4.staticflickr.com\\/3082\\/2716691340_2ea1c206aa.jpg",
};
Intent intent = new Intent(this, MyServer.class);
intent.putExtra("URL", imgURLs[i].toString());
startService(intent);
}
}
https://developer.android.com/guide/components/services.html
Develop > API Guides > 应用组件
//////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.e560.m1217a">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyServer"></service>
</application>
</manifest>
//////////////////////////////////////////////////////////////////////
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.LinearLayout;
public class MainActivity extends AppCompatActivity {
private int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
Button bt = new Button(this);
bt.setText("SaveFiletoAndroidFile");
linearLayout.addView(bt);
setContentView(linearLayout);
i = 0;
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i++;
SavetoServer(i);
}
});
}
protected void SavetoServer(int i) {
String[] imgURLs = new String[]{
"https://farm4.staticflickr.com/3239/2897452239_d2cf730467.jpg",
"https:\\/\\/farm4.staticflickr.com\\/3050\\/2897460805_781ed40c84.jpg",
"https:\\/\\/farm4.staticflickr.com\\/3553\\/3619665782_e9766aefe5.jpg",
"https:\\/\\/farm4.staticflickr.com\\/3082\\/2716691340_2ea1c206aa.jpg",
"https:\\/\\/farm4.staticflickr.com\\/3082\\/2716691340_2ea1c206aa.jpg",
};
Intent intent = new Intent(this, MyServer.class);
intent.putExtra("URL", imgURLs[i].toString());
startService(intent);
}
}
//////////////////////////////////////////////////////////////////////
import android.app.IntentService;
import android.content.Intent;
public class MyServer extends IntentService {
public MyServer() {
super("test");
}
@Override
protected void onHandleIntent(Intent intent) {
HTTPClient httpClient = new HTTPClient(getApplicationContext(), intent.getStringExtra("URL"));
}
}
//////////////////////////////////////////////////////////////////////
import android.content.Context;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HTTPClient {
private String urlString;
private String FileName;
private String savePath;
private Context context;
public HTTPClient(Context context, String urlString) {
this.context = context;
this.urlString = urlString.replace("\\", "");
this.savePath = String.valueOf(this.context.getFilesDir());
FileName = getFname(urlString);
System.out.println("Thread.name Httpclient" + Thread.currentThread().getName());
SaveFile();
}
public void SaveFile() {
try {
URL url = new URL(urlString);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(5 * 1000);
httpURLConnection.setReadTimeout(5 * 1000);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
if (httpURLConnection.getResponseCode() == 200) {
File SaveF = new File(this.savePath + "/" + this.FileName);
InputStream inputStream = httpURLConnection.getInputStream();
FileOutputStream fos = new FileOutputStream(SaveF);
byte[] buff = new byte[4096];
int len = 0;
while ((len = inputStream.read(buff)) != -1) {
fos.write(buff, 0, len);
fos.flush();
}
fos.close();
inputStream.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected String getFname(String url) {
String[] temp = url.split("/");
return temp[temp.length - 1];
}
}
2016年12月9日金曜日
2016年12月8日木曜日
Java FTP Socket
Java FTP Socket
添加对文件夹的处理。
import java.io.*;
import java.net.*;
/*
file:///E:/api/index.html
*/
class Tsocket {
public static void main(String[] args) {
try {
// File[] fs = new File[] {new File("Sublime Text Build 3114 x64.zip"), new File("Sublime Text Build 3114 x64 (2).zip"), new File("npp.6.9.2.bin.zip"), new File("Sublime Text Build 3114.zip")};
File f = new File("E:\\Android");
File[] fs = f.listFiles() ;
// UFTP u = new UFTP("xxxx.web.fc2.com", "xxxx", "xxxx");
UFTP u = new UFTP("192.168.11.7", "guest", "guest");
String code = u.FTPconnect();
if (code.equals("ConnectFTPServer")) {
u.upload(fs);
u.close();
System.out.println(code);
} else {
System.out.println(code);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class UFTP {
private String USER, PASSWORD;
private InetAddress FTPServerIP;
private BufferedReader FTPCmdreader;
private BufferedWriter FTPCmdwriter ;
public UFTP(String FURL, String USER, String PASSWORD) throws Exception {
this.FTPServerIP = InetAddress.getByName(FURL);
this.USER = USER;
this.PASSWORD = PASSWORD;
}
public String FTPconnect() {
String errorlevel = "ConnectFTPServer";
try {
Socket socket21 = new Socket (FTPServerIP, 21);
FTPCmdreader = new BufferedReader(new InputStreamReader(socket21.getInputStream()));
FTPCmdwriter = new BufferedWriter(new OutputStreamWriter(socket21.getOutputStream()));
FTPcommand(FTPCmdwriter, "USER " + USER);
FTPcommand(FTPCmdwriter, "PASS " + PASSWORD);
} catch (Exception e) {
errorlevel = "ConnectFTPerror";
}
return errorlevel;
}
public void upload(File[] files) throws Exception {
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
FTPcommand(FTPCmdwriter, "PASV ");
String[] temp = new String[] {};
String recode = new String();
while (true) {
recode = FTPCmdreader.readLine();
System.out.println(recode);
if (recode.substring(0, 3).equals("227")) {
temp = get227(recode);
break;
}
}
Socket socket20 = new Socket(temp[0], Integer.parseInt(temp[1]));
OutputStream ftpOutputStream = socket20.getOutputStream();
FileInputStream upfileInputStream = new FileInputStream(files[i]);
FTPcommand(FTPCmdwriter, "TYPE " + "I");
FTPcommand(FTPCmdwriter, "STOR " + files[i].getName());
System.out.println("UPDATE Filename " + files[i].getName());
byte[] buff = new byte[1024 * 1024];
int len = 0;
while ( (len = upfileInputStream.read(buff) ) != -1) {
ftpOutputStream.write(buff, 0, len);
ftpOutputStream.flush();
System.out.print("*");
}
ftpOutputStream.close();
upfileInputStream.close();
System.out.println("");
Thread.sleep(5 * 1000);
}
}
}
public void close() {
try {
FTPcommand(FTPCmdwriter, "QUIT ");
String temp = null;
while ( (temp = FTPCmdreader.readLine() ) != null) {
System.out.println(temp);
}
System.out.println("");
FTPCmdwriter.close();
FTPCmdreader.close();
} catch (Exception e) {
}
}
private void FTPcommand(BufferedWriter FTPCmdwriter, String FTPcmd) throws Exception {
FTPCmdwriter.write(FTPcmd + "\r\n");
FTPCmdwriter.flush();
}
protected String[] get227(String string) {
int start = string.indexOf("(") + 1;
int end = string.indexOf(")");
String substring = string.substring(start, end);
String[] temp = substring.split(",");
String ip = temp[0] + "." + temp[1] + "." + temp[2] + "." + temp[3];
int port = Integer.parseInt(temp[4]) * 256 + Integer.parseInt(temp[5]);
String sport = String.valueOf(port);
String[] res = {ip, sport};
return res;
}
}
添加对文件夹的处理。
import java.io.*;
import java.net.*;
/*
file:///E:/api/index.html
*/
class Tsocket {
public static void main(String[] args) {
try {
// File[] fs = new File[] {new File("Sublime Text Build 3114 x64.zip"), new File("Sublime Text Build 3114 x64 (2).zip"), new File("npp.6.9.2.bin.zip"), new File("Sublime Text Build 3114.zip")};
File f = new File("E:\\Android");
File[] fs = f.listFiles() ;
// UFTP u = new UFTP("xxxx.web.fc2.com", "xxxx", "xxxx");
UFTP u = new UFTP("192.168.11.7", "guest", "guest");
String code = u.FTPconnect();
if (code.equals("ConnectFTPServer")) {
u.upload(fs);
u.close();
System.out.println(code);
} else {
System.out.println(code);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class UFTP {
private String USER, PASSWORD;
private InetAddress FTPServerIP;
private BufferedReader FTPCmdreader;
private BufferedWriter FTPCmdwriter ;
public UFTP(String FURL, String USER, String PASSWORD) throws Exception {
this.FTPServerIP = InetAddress.getByName(FURL);
this.USER = USER;
this.PASSWORD = PASSWORD;
}
public String FTPconnect() {
String errorlevel = "ConnectFTPServer";
try {
Socket socket21 = new Socket (FTPServerIP, 21);
FTPCmdreader = new BufferedReader(new InputStreamReader(socket21.getInputStream()));
FTPCmdwriter = new BufferedWriter(new OutputStreamWriter(socket21.getOutputStream()));
FTPcommand(FTPCmdwriter, "USER " + USER);
FTPcommand(FTPCmdwriter, "PASS " + PASSWORD);
} catch (Exception e) {
errorlevel = "ConnectFTPerror";
}
return errorlevel;
}
public void upload(File[] files) throws Exception {
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
FTPcommand(FTPCmdwriter, "PASV ");
String[] temp = new String[] {};
String recode = new String();
while (true) {
recode = FTPCmdreader.readLine();
System.out.println(recode);
if (recode.substring(0, 3).equals("227")) {
temp = get227(recode);
break;
}
}
Socket socket20 = new Socket(temp[0], Integer.parseInt(temp[1]));
OutputStream ftpOutputStream = socket20.getOutputStream();
FileInputStream upfileInputStream = new FileInputStream(files[i]);
FTPcommand(FTPCmdwriter, "TYPE " + "I");
FTPcommand(FTPCmdwriter, "STOR " + files[i].getName());
System.out.println("UPDATE Filename " + files[i].getName());
byte[] buff = new byte[1024 * 1024];
int len = 0;
while ( (len = upfileInputStream.read(buff) ) != -1) {
ftpOutputStream.write(buff, 0, len);
ftpOutputStream.flush();
System.out.print("*");
}
ftpOutputStream.close();
upfileInputStream.close();
System.out.println("");
Thread.sleep(5 * 1000);
}
}
}
public void close() {
try {
FTPcommand(FTPCmdwriter, "QUIT ");
String temp = null;
while ( (temp = FTPCmdreader.readLine() ) != null) {
System.out.println(temp);
}
System.out.println("");
FTPCmdwriter.close();
FTPCmdreader.close();
} catch (Exception e) {
}
}
private void FTPcommand(BufferedWriter FTPCmdwriter, String FTPcmd) throws Exception {
FTPCmdwriter.write(FTPcmd + "\r\n");
FTPCmdwriter.flush();
}
protected String[] get227(String string) {
int start = string.indexOf("(") + 1;
int end = string.indexOf(")");
String substring = string.substring(start, end);
String[] temp = substring.split(",");
String ip = temp[0] + "." + temp[1] + "." + temp[2] + "." + temp[3];
int port = Integer.parseInt(temp[4]) * 256 + Integer.parseInt(temp[5]);
String sport = String.valueOf(port);
String[] res = {ip, sport};
return res;
}
}
登録:
投稿 (Atom)