很久以前寫過的一個Android程序,主要用到了ListView,ContentProvider,solite數據庫和Java網絡通信實現了基於第三方API的飛信群發助手,實現了飛信的添加好友,發送飛信和飛信群發功能,可以作為參考,源碼我已經上傳,下載地址我會放在下面,供大家下載學習。本人小菜一個。。。。歡迎大家拍磚~~~
這是程序的主界面:
主界面源代碼,FetionSend.java
- package com.huangcheng.mainFrame;
-
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.regex.Pattern;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.app.AlertDialog.Builder;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
-
- import com.huangcheng.FetionAdd.AddPerson;
- import com.huangcheng.FetionSend.SendPerson;
-
- public class FetionSend extends Activity {
- /** Called when the activity is first created. */
-
- // private final boolean state=false;;
-
- private final int test_fail = 0;
- private final int test_true = 1;
-
- private Button add, send, quit, save;
- private EditText user, pwd;
- private String username, password;
-
- private ProgressDialog progressDialog;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- add = (Button) findViewById(R.id.AddPeople);
- send = (Button) findViewById(R.id.SendPeople);
- quit = (Button) findViewById(R.id.Quit);
- save = (Button) findViewById(R.id.SaveUser);
- user = (EditText) findViewById(R.id.feitionnumber);
- pwd = (EditText) findViewById(R.id.feitionpwd);
- SharedPreferences sharedPreferences = getSharedPreferences(
- "Fetion_Send", Context.MODE_PRIVATE);
- username = sharedPreferences.getString("username", "");
- user.setText(username);
- password = sharedPreferences.getString("password", "");
- pwd.setText(password);
- save.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- TestLogin();
- }
-
- });
- add.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- SharedPreferences sharedPreferences_test = getSharedPreferences(
- "test", Context.MODE_PRIVATE);
- boolean test = sharedPreferences_test.getBoolean("test", false);
- if (user.getText().length() == 0 || pwd.getText().length() == 0
- || isNumeric(user.getText().toString()) || !test) {
- AlertDialog.Builder builder = new Builder(FetionSend.this);
- builder.setMessage("您飛信用戶名或者密碼輸入有誤,確認要添加飛信好友麼?");
- builder.setTitle("提示");
- builder.setPositiveButton("確認",
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog,
- int arg1) {
- // TODO Auto-generated method stub
- dialog.dismiss();
- Intent intent = new Intent();
- intent.setClass(FetionSend.this,
- AddPerson.class);
- startActivity(intent);
- }
- });
- builder.setNegativeButton("取消",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog,
- int arg1) {
- // TODO Auto-generated method stub
- dialog.dismiss();
- }
- });
- builder.create().show();
- } else {
- Intent intent = new Intent();
- intent.setClass(FetionSend.this, AddPerson.class);
- startActivity(intent);
- }
- }
- });
- send.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- SharedPreferences sharedPreferences_test = getSharedPreferences(
- "test", Context.MODE_PRIVATE);
- boolean test = sharedPreferences_test.getBoolean("test", false);
- if (user.getText().length() == 0 || pwd.getText().length() == 0
- || isNumeric(user.getText().toString()) || !test) {
- AlertDialog.Builder builder = new Builder(FetionSend.this);
- builder.setMessage("您飛信用戶名或者密碼輸入有誤,確認要發送飛信麼?");
- builder.setTitle("提示");
- builder.setPositiveButton("確認",
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog,
- int arg1) {
- // TODO Auto-generated method stub
- dialog.dismiss();
- Intent intent = new Intent();
- intent.setClass(FetionSend.this,
- SendPerson.class);
- startActivity(intent);
- }
- });
- builder.setNegativeButton("取消",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog,
- int arg1) {
- // TODO Auto-generated method stub
- dialog.dismiss();
- }
- });
- builder.create().show();
- } else {
- Intent intent = new Intent();
- intent.setClass(FetionSend.this, SendPerson.class);
- startActivity(intent);
- }
- }
- });
- quit.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- AlertDialog.Builder builder = new Builder(FetionSend.this);
- builder.setMessage("確認退出嗎?");
- builder.setTitle("提示");
- builder.setPositiveButton("確認",
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int arg1) {
- // TODO Auto-generated method stub
- dialog.dismiss();
- FetionSend.this.finish();
- System.exit(0);
- }
- });
- builder.setNegativeButton("取消",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int arg1) {
- // TODO Auto-generated method stub
- dialog.dismiss();
- }
- });
- builder.create().show();
- }
- });
- }
-
- // @Override
- // protected void onStop() {
- // // TODO Auto-generated method stub
- // super.onStop();
- // SharedPreferences
- // sharedPreferences_save=getSharedPreferences("Fetion_Send",Context.MODE_PRIVATE);
- // Editor editor=sharedPreferences_save.edit();
- // editor.putString("username",user.getText().toString());
- // editor.putString("password",pwd.getText().toString());
- // }
- private final Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- switch (msg.what) {
- case test_fail:
- // 添加失敗
- progressDialog.dismiss();
- Toast.makeText(FetionSend.this, "存儲飛信號和密碼失敗,飛信號或密碼錯誤,請重試!",
- Toast.LENGTH_LONG).show();
- break;
- case test_true:
- // 添加失敗
- progressDialog.dismiss();
- Toast.makeText(FetionSend.this, "存儲飛信號和密碼成功", Toast.LENGTH_LONG)
- .show();
- break;
- }
- };
- };
-
- public static boolean isNumeric(String str) {
- Pattern pattern = Pattern.compile("[0-9]*");
- return !pattern.matcher(str).matches();
- }
-
- public void TestLogin() {
- progressDialog = ProgressDialog.show(FetionSend.this, "聯網中...",
- "請稍等,正在驗證飛信號和密碼...");
- SharedPreferences sharedPreferences_save = getSharedPreferences(
- "Fetion_Send", Context.MODE_PRIVATE);
- Editor editor_save = sharedPreferences_save.edit();
- editor_save.putString("username", user.getText().toString());
- editor_save.putString("password", pwd.getText().toString());
- editor_save.commit();
- username = sharedPreferences_save.getString("username", "");
- password = sharedPreferences_save.getString("password", "");
- Runnable r = new Runnable() {
-
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- String resultData = "";
- String httpurl = "http://lab.3haku.net/f/do?phone="
- + username + "&pwd=" + password + "&add="
- + username;
- URL url = new URL(httpurl);
- // 使用HttpURLConnection打開連接
- HttpURLConnection urlConn = (HttpURLConnection) url
- .openConnection();
- // 得到讀取的內容(流)
- InputStreamReader in = new InputStreamReader(
- urlConn.getInputStream());
- // 為輸出創建BufferedReader
- BufferedReader buffer = new BufferedReader(in);
- String inputLine = null;
- // 使用循環來讀取獲得的數據
- while (((inputLine = buffer.readLine()) != null)) {
- // 我們在每一行後面加上一個"\n"來換行
- resultData += inputLine + "\n";
- }
- // 關閉InputStreamReader
- in.close();
- // 關閉http連接
- urlConn.disconnect();
- System.out.println(resultData.substring(86, 93));
- System.out.println(resultData);
- if (resultData.substring(86, 93).equals("success")) {
- SharedPreferences sharedPreferences_test = getSharedPreferences(
- "test", Context.MODE_PRIVATE);
- Editor editor = sharedPreferences_test.edit();
- editor.putBoolean("test", true);
- editor.commit();
- Message msg = handler.obtainMessage();
- msg.what = test_true;
- handler.sendMessage(msg);
- SharedPreferences sharedPreferences_save = getSharedPreferences(
- "Fetion_Send", Context.MODE_PRIVATE);
- Editor editor_save = sharedPreferences_save.edit();
- editor_save.putString("username", user.getText()
- .toString());
- editor_save.putString("password", pwd.getText()
- .toString());
- editor_save.commit();
-
- } else {
- // SharedPreferences
- // sharedPreferences_test=getSharedPreferences("test",Context.MODE_PRIVATE);
- // Editor editor=sharedPreferences_test.edit();
- // editor.putBoolean("test", false);
- // editor.commit();
- Message msg = handler.obtainMessage();
- msg.what = test_fail;
- handler.sendMessage(msg);
- }
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- };
- Thread testThread = new Thread(r);
- testThread.start();
- }
- }
這個界面展示了基本所有功能,驗證你的飛信號碼和密碼是否正確,是否要添加飛信好友,是否要進行飛信群發。