Androidstdio启动一服务,使产品价格每秒涨价一元,并实时更新界面总代码
时间: 2023-12-27 10:03:05 浏览: 74
androidstdio学生选课系统
好的,下面是一个完整的示例代码,用于实现您的需求:
MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
private TextView mPriceTextView;
private Button mStartButton;
private Button mStopButton;
private BroadcastReceiver mPriceChangedBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// 更新价格文本框的值
double price = intent.getDoubleExtra("price", 0.0);
mPriceTextView.setText(String.format(Locale.getDefault(), "%.2f", price));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取 UI 控件
mPriceTextView = findViewById(R.id.price_text_view);
mStartButton = findViewById(R.id.start_button);
mStopButton = findViewById(R.id.stop_button);
// 设置按钮点击事件监听器
mStartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 启动服务
startPriceIncreaseService();
}
});
mStopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 停止服务
stopPriceIncreaseService();
}
});
// 注册广播接收器
registerReceiver(mPriceChangedBroadcastReceiver, new IntentFilter("com.example.price_changed"));
}
@Override
protected void onDestroy() {
super.onDestroy();
// 停止服务
stopPriceIncreaseService();
// 注销广播接收器
unregisterReceiver(mPriceChangedBroadcastReceiver);
}
// 启动服务
private void startPriceIncreaseService() {
Intent intent = new Intent(this, PriceIncreaseService.class);
startService(intent);
}
// 停止服务
private void stopPriceIncreaseService() {
Intent intent = new Intent(this, PriceIncreaseService.class);
stopService(intent);
}
}
```
PriceIncreaseService.java
```java
public class PriceIncreaseService extends Service {
private static final String TAG = "PriceIncreaseService";
private static final long PRICE_INCREASE_INTERVAL = 1000; // 价格每秒钟增加的间隔
private Timer mPriceIncreaseTimer; // 计时器
private double mPrice = 0.0; // 初始价格
// 广播接收器,用于通知 UI 界面价格变化的事件
private BroadcastReceiver mPriceChangedBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// 发送广播通知 UI 界面价格变化的事件
sendPriceChangedBroadcast();
}
};
@Override
public void onCreate() {
super.onCreate();
// 注册广播接收器
registerReceiver(mPriceChangedBroadcastReceiver, new IntentFilter("com.example.price_changed"));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 启动计时器
startPriceIncreaseTimer();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
// 停止计时器
stopPriceIncreaseTimer();
// 注销广播接收器
unregisterReceiver(mPriceChangedBroadcastReceiver);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
// 启动计时器
private void startPriceIncreaseTimer() {
mPriceIncreaseTimer = new Timer();
mPriceIncreaseTimer.schedule(new TimerTask() {
@Override
public void run() {
mPrice += 1.0;
// 发送广播通知价格变化的事件
sendPriceChangedBroadcast();
}
}, PRICE_INCREASE_INTERVAL, PRICE_INCREASE_INTERVAL);
}
// 停止计时器
private void stopPriceIncreaseTimer() {
if (mPriceIncreaseTimer != null) {
mPriceIncreaseTimer.cancel();
mPriceIncreaseTimer = null;
}
}
// 发送广播通知价格变化的事件
private void sendPriceChangedBroadcast() {
Intent intent = new Intent("com.example.price_changed");
intent.putExtra("price", mPrice);
sendBroadcast(intent);
}
}
```
在上面的示例代码中,MainActivity 类负责处理 UI 界面相关的逻辑,包括启动和停止服务以及更新价格文本框的值。PriceIncreaseService 类则负责处理价格自动增加的逻辑,并在价格变化时发送广播通知 UI 界面价格变化的事件。
请注意,上面的代码仅仅是一个示例,并不是完整的实现。您需要根据您自己的需求进行调整和完善。
阅读全文