| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- from kivy.app import App
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.label import Label
- from kivy.uix.button import Button
- from kivy.clock import Clock
- import mysql.connector
- # 调用Android的API
- SmsManager = autoclass('android.telephony.SmsManager')
- PythonActivity = autoclass('org.kivy.android.PythonActivity')
- activity = PythonActivity.mActivity
- # MySQL数据库连接配置
- db_config = {
- 'host': 'your_mysql_host', # 例如 'localhost' 或 '192.168.1.100'
- 'user': 'your_mysql_user', # 例如 'root'
- 'password': 'your_mysql_password', # 例如 'password'
- 'database': 'your_database_name', # 例如 'sms_db'
- }
- # 获取数据库连接
- def get_db_connection():
- return mysql.connector.connect(**db_config)
- # 从MySQL数据库获取数据
- def get_data_from_db():
- conn = get_db_connection()
- cursor = conn.cursor()
- cursor.execute("SELECT phone_number, message FROM messages WHERE sent = 0")
- data = cursor.fetchall()
- cursor.close()
- conn.close()
- return data
- # 更新数据库状态
- def update_db_status(phone_number):
- conn = get_db_connection()
- cursor = conn.cursor()
- cursor.execute("UPDATE messages SET sent = 1 WHERE phone_number = %s", (phone_number,))
- conn.commit()
- cursor.close()
- conn.close()
- # 发送短信
- def send_sms(phone_number, message):
- sms_manager = SmsManager.getDefault()
- sms_manager.sendTextMessage(phone_number, None, message, None, None)
- class SmsApp(App):
- def build(self):
- self.layout = BoxLayout(orientation='vertical')
- self.label = Label(text="SMS Sender")
- self.layout.add_widget(self.label)
- self.start_button = Button(text="Start Sending SMS")
- self.start_button.bind(on_press=self.start_sending)
- self.layout.add_widget(self.start_button)
- return self.layout
- def start_sending(self, instance):
- self.label.text = "Started SMS Sending Service..."
- # 每10秒调用一次 send_sms_loop
- Clock.schedule_interval(self.send_sms_loop, 10) # 10秒间隔
- def send_sms_loop(self, dt):
- data = get_data_from_db()
- if data:
- for phone_number, message in data:
- try:
- send_sms(phone_number, message)
- update_db_status(phone_number)
- self.label.text = f"SMS sent to {phone_number}"
- except Exception as e:
- self.label.text = f"Failed to send SMS: {e}"
- else:
- self.label.text = "No more messages to send"
- if __name__ == '__main__':
- SmsApp().run()
|