歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android飛信群發助手【附源碼】

很久以前寫過的一個Android程序,主要用到了ListView,ContentProvider,solite數據庫和Java網絡通信實現了基於第三方API的飛信群發助手,實現了飛信的添加好友,發送飛信和飛信群發功能,可以作為參考,源碼我已經上傳,下載地址我會放在下面,供大家下載學習。本人小菜一個。。。。歡迎大家拍磚~~~

這是程序的主界面:

主界面源代碼,FetionSend.java

  1. package com.huangcheng.mainFrame;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.InputStreamReader;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7. import java.util.regex.Pattern;  
  8.   
  9. import android.app.Activity;  
  10. import android.app.AlertDialog;  
  11. import android.app.ProgressDialog;  
  12. import android.app.AlertDialog.Builder;  
  13. import android.content.Context;  
  14. import android.content.DialogInterface;  
  15. import android.content.Intent;  
  16. import android.content.SharedPreferences;  
  17. import android.content.SharedPreferences.Editor;  
  18. import android.os.Bundle;  
  19. import android.os.Handler;  
  20. import android.os.Message;  
  21. import android.view.View;  
  22. import android.view.View.OnClickListener;  
  23. import android.widget.Button;  
  24. import android.widget.EditText;  
  25. import android.widget.Toast;  
  26.   
  27. import com.huangcheng.FetionAdd.AddPerson;  
  28. import com.huangcheng.FetionSend.SendPerson;  
  29.   
  30. public class FetionSend extends Activity {  
  31.     /** Called when the activity is first created. */  
  32.   
  33.     // private final boolean state=false;;   
  34.   
  35.     private final int test_fail = 0;  
  36.     private final int test_true = 1;  
  37.   
  38.     private Button add, send, quit, save;  
  39.     private EditText user, pwd;  
  40.     private String username, password;  
  41.   
  42.     private ProgressDialog progressDialog;  
  43.   
  44.     @Override  
  45.     public void onCreate(Bundle savedInstanceState) {  
  46.         super.onCreate(savedInstanceState);  
  47.         setContentView(R.layout.main);  
  48.         add = (Button) findViewById(R.id.AddPeople);  
  49.         send = (Button) findViewById(R.id.SendPeople);  
  50.         quit = (Button) findViewById(R.id.Quit);  
  51.         save = (Button) findViewById(R.id.SaveUser);  
  52.         user = (EditText) findViewById(R.id.feitionnumber);  
  53.         pwd = (EditText) findViewById(R.id.feitionpwd);  
  54.         SharedPreferences sharedPreferences = getSharedPreferences(  
  55.                 "Fetion_Send", Context.MODE_PRIVATE);  
  56.         username = sharedPreferences.getString("username""");  
  57.         user.setText(username);  
  58.         password = sharedPreferences.getString("password""");  
  59.         pwd.setText(password);  
  60.         save.setOnClickListener(new OnClickListener() {  
  61.   
  62.             @Override  
  63.             public void onClick(View arg0) {  
  64.                 // TODO Auto-generated method stub   
  65.                 TestLogin();  
  66.             }  
  67.   
  68.         });  
  69.         add.setOnClickListener(new OnClickListener() {  
  70.   
  71.             @Override  
  72.             public void onClick(View arg0) {  
  73.                 // TODO Auto-generated method stub   
  74.                 SharedPreferences sharedPreferences_test = getSharedPreferences(  
  75.                         "test", Context.MODE_PRIVATE);  
  76.                 boolean test = sharedPreferences_test.getBoolean("test"false);  
  77.                 if (user.getText().length() == 0 || pwd.getText().length() == 0  
  78.                         || isNumeric(user.getText().toString()) || !test) {  
  79.                     AlertDialog.Builder builder = new Builder(FetionSend.this);  
  80.                     builder.setMessage("您飛信用戶名或者密碼輸入有誤,確認要添加飛信好友麼?");  
  81.                     builder.setTitle("提示");  
  82.                     builder.setPositiveButton("確認",  
  83.                             new DialogInterface.OnClickListener() {  
  84.   
  85.                                 @Override  
  86.                                 public void onClick(DialogInterface dialog,  
  87.                                         int arg1) {  
  88.                                     // TODO Auto-generated method stub   
  89.                                     dialog.dismiss();  
  90.                                     Intent intent = new Intent();  
  91.                                     intent.setClass(FetionSend.this,  
  92.                                             AddPerson.class);  
  93.                                     startActivity(intent);  
  94.                                 }  
  95.                             });  
  96.                     builder.setNegativeButton("取消",  
  97.                             new DialogInterface.OnClickListener() {  
  98.                                 @Override  
  99.                                 public void onClick(DialogInterface dialog,  
  100.                                         int arg1) {  
  101.                                     // TODO Auto-generated method stub   
  102.                                     dialog.dismiss();  
  103.                                 }  
  104.                             });  
  105.                     builder.create().show();  
  106.                 } else {  
  107.                     Intent intent = new Intent();  
  108.                     intent.setClass(FetionSend.this, AddPerson.class);  
  109.                     startActivity(intent);  
  110.                 }  
  111.             }  
  112.         });  
  113.         send.setOnClickListener(new OnClickListener() {  
  114.   
  115.             @Override  
  116.             public void onClick(View arg0) {  
  117.                 // TODO Auto-generated method stub   
  118.                 SharedPreferences sharedPreferences_test = getSharedPreferences(  
  119.                         "test", Context.MODE_PRIVATE);  
  120.                 boolean test = sharedPreferences_test.getBoolean("test"false);  
  121.                 if (user.getText().length() == 0 || pwd.getText().length() == 0  
  122.                         || isNumeric(user.getText().toString()) || !test) {  
  123.                     AlertDialog.Builder builder = new Builder(FetionSend.this);  
  124.                     builder.setMessage("您飛信用戶名或者密碼輸入有誤,確認要發送飛信麼?");  
  125.                     builder.setTitle("提示");  
  126.                     builder.setPositiveButton("確認",  
  127.                             new DialogInterface.OnClickListener() {  
  128.   
  129.                                 @Override  
  130.                                 public void onClick(DialogInterface dialog,  
  131.                                         int arg1) {  
  132.                                     // TODO Auto-generated method stub   
  133.                                     dialog.dismiss();  
  134.                                     Intent intent = new Intent();  
  135.                                     intent.setClass(FetionSend.this,  
  136.                                             SendPerson.class);  
  137.                                     startActivity(intent);  
  138.                                 }  
  139.                             });  
  140.                     builder.setNegativeButton("取消",  
  141.                             new DialogInterface.OnClickListener() {  
  142.                                 @Override  
  143.                                 public void onClick(DialogInterface dialog,  
  144.                                         int arg1) {  
  145.                                     // TODO Auto-generated method stub   
  146.                                     dialog.dismiss();  
  147.                                 }  
  148.                             });  
  149.                     builder.create().show();  
  150.                 } else {  
  151.                     Intent intent = new Intent();  
  152.                     intent.setClass(FetionSend.this, SendPerson.class);  
  153.                     startActivity(intent);  
  154.                 }  
  155.             }  
  156.         });  
  157.         quit.setOnClickListener(new OnClickListener() {  
  158.   
  159.             @Override  
  160.             public void onClick(View arg0) {  
  161.                 // TODO Auto-generated method stub   
  162.                 AlertDialog.Builder builder = new Builder(FetionSend.this);  
  163.                 builder.setMessage("確認退出嗎?");  
  164.                 builder.setTitle("提示");  
  165.                 builder.setPositiveButton("確認",  
  166.                         new DialogInterface.OnClickListener() {  
  167.   
  168.                             @Override  
  169.                             public void onClick(DialogInterface dialog, int arg1) {  
  170.                                 // TODO Auto-generated method stub   
  171.                                 dialog.dismiss();  
  172.                                 FetionSend.this.finish();  
  173.                                 System.exit(0);  
  174.                             }  
  175.                         });  
  176.                 builder.setNegativeButton("取消",  
  177.                         new DialogInterface.OnClickListener() {  
  178.                             @Override  
  179.                             public void onClick(DialogInterface dialog, int arg1) {  
  180.                                 // TODO Auto-generated method stub   
  181.                                 dialog.dismiss();  
  182.                             }  
  183.                         });  
  184.                 builder.create().show();  
  185.             }  
  186.         });  
  187.     }  
  188.   
  189.     // @Override   
  190.     // protected void onStop() {   
  191.     // // TODO Auto-generated method stub   
  192.     // super.onStop();   
  193.     // SharedPreferences   
  194.     // sharedPreferences_save=getSharedPreferences("Fetion_Send",Context.MODE_PRIVATE);   
  195.     // Editor editor=sharedPreferences_save.edit();   
  196.     // editor.putString("username",user.getText().toString());   
  197.     // editor.putString("password",pwd.getText().toString());   
  198.     // }   
  199.     private final Handler handler = new Handler() {  
  200.         public void handleMessage(Message msg) {  
  201.             super.handleMessage(msg);  
  202.             switch (msg.what) {  
  203.             case test_fail:  
  204.                 // 添加失敗   
  205.                 progressDialog.dismiss();  
  206.                 Toast.makeText(FetionSend.this"存儲飛信號和密碼失敗,飛信號或密碼錯誤,請重試!",  
  207.                         Toast.LENGTH_LONG).show();  
  208.                 break;  
  209.             case test_true:  
  210.                 // 添加失敗   
  211.                 progressDialog.dismiss();  
  212.                 Toast.makeText(FetionSend.this"存儲飛信號和密碼成功", Toast.LENGTH_LONG)  
  213.                         .show();  
  214.                 break;  
  215.             }  
  216.         };  
  217.     };  
  218.   
  219.     public static boolean isNumeric(String str) {  
  220.         Pattern pattern = Pattern.compile("[0-9]*");  
  221.         return !pattern.matcher(str).matches();  
  222.     }  
  223.   
  224.     public void TestLogin() {  
  225.         progressDialog = ProgressDialog.show(FetionSend.this"聯網中...",  
  226.                 "請稍等,正在驗證飛信號和密碼...");  
  227.         SharedPreferences sharedPreferences_save = getSharedPreferences(  
  228.                 "Fetion_Send", Context.MODE_PRIVATE);  
  229.         Editor editor_save = sharedPreferences_save.edit();  
  230.         editor_save.putString("username", user.getText().toString());  
  231.         editor_save.putString("password", pwd.getText().toString());  
  232.         editor_save.commit();  
  233.         username = sharedPreferences_save.getString("username""");  
  234.         password = sharedPreferences_save.getString("password""");  
  235.         Runnable r = new Runnable() {  
  236.   
  237.             @Override  
  238.             public void run() {  
  239.                 // TODO Auto-generated method stub   
  240.                 try {  
  241.                     String resultData = "";  
  242.                     String httpurl = "http://lab.3haku.net/f/do?phone="  
  243.                             + username + "&pwd=" + password + "&add="  
  244.                             + username;  
  245.                     URL url = new URL(httpurl);  
  246.                     // 使用HttpURLConnection打開連接   
  247.                     HttpURLConnection urlConn = (HttpURLConnection) url  
  248.                             .openConnection();  
  249.                     // 得到讀取的內容(流)   
  250.                     InputStreamReader in = new InputStreamReader(  
  251.                             urlConn.getInputStream());  
  252.                     // 為輸出創建BufferedReader   
  253.                     BufferedReader buffer = new BufferedReader(in);  
  254.                     String inputLine = null;  
  255.                     // 使用循環來讀取獲得的數據   
  256.                     while (((inputLine = buffer.readLine()) != null)) {  
  257.                         // 我們在每一行後面加上一個"\n"來換行   
  258.                         resultData += inputLine + "\n";  
  259.                     }  
  260.                     // 關閉InputStreamReader   
  261.                     in.close();  
  262.                     // 關閉http連接   
  263.                     urlConn.disconnect();  
  264.                     System.out.println(resultData.substring(8693));  
  265.                     System.out.println(resultData);  
  266.                     if (resultData.substring(8693).equals("success")) {  
  267.                         SharedPreferences sharedPreferences_test = getSharedPreferences(  
  268.                                 "test", Context.MODE_PRIVATE);  
  269.                         Editor editor = sharedPreferences_test.edit();  
  270.                         editor.putBoolean("test"true);  
  271.                         editor.commit();  
  272.                         Message msg = handler.obtainMessage();  
  273.                         msg.what = test_true;  
  274.                         handler.sendMessage(msg);  
  275.                         SharedPreferences sharedPreferences_save = getSharedPreferences(  
  276.                                 "Fetion_Send", Context.MODE_PRIVATE);  
  277.                         Editor editor_save = sharedPreferences_save.edit();  
  278.                         editor_save.putString("username", user.getText()  
  279.                                 .toString());  
  280.                         editor_save.putString("password", pwd.getText()  
  281.                                 .toString());  
  282.                         editor_save.commit();  
  283.   
  284.                     } else {  
  285.                         // SharedPreferences   
  286.                         // sharedPreferences_test=getSharedPreferences("test",Context.MODE_PRIVATE);   
  287.                         // Editor editor=sharedPreferences_test.edit();   
  288.                         // editor.putBoolean("test", false);   
  289.                         // editor.commit();   
  290.                         Message msg = handler.obtainMessage();  
  291.                         msg.what = test_fail;  
  292.                         handler.sendMessage(msg);  
  293.                     }  
  294.                 } catch (Exception e) {  
  295.                     // TODO: handle exception   
  296.                     e.printStackTrace();  
  297.                 }  
  298.             }  
  299.         };  
  300.         Thread testThread = new Thread(r);  
  301.         testThread.start();  
  302.     }  
  303. }  
這個界面展示了基本所有功能,驗證你的飛信號碼和密碼是否正確,是否要添加飛信好友,是否要進行飛信群發。
Copyright © Linux教程網 All Rights Reserved