Androidstudio每五秒改变一次价格并且通知代码完整
时间: 2023-07-02 22:09:36 浏览: 57
以下是一个Android Studio的示例代码,每五秒钟更新价格并显示在TextView中,并通过通知显示完整的代码:
```java
public class MainActivity extends AppCompatActivity {
private TextView priceTextView;
private Handler handler;
private Runnable runnable;
private NotificationManagerCompat notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
priceTextView = findViewById(R.id.price_text_view);
notificationManager = NotificationManagerCompat.from(this);
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
// 每五秒钟更新价格
double price = Math.round(Math.random() * 10000.0) / 100.0;
priceTextView.setText("$" + price);
// 显示通知
String code = "public class MainActivity {\n" +
" private TextView priceTextView;\n" +
" private Handler handler;\n" +
" private Runnable runnable;\n" +
" private NotificationManagerCompat notificationManager;\n\n" +
" @Override\n" +
" protected void onCreate(Bundle savedInstanceState) {\n" +
" super.onCreate(savedInstanceState);\n" +
" setContentView(R.layout.activity_main);\n\n" +
" priceTextView = findViewById(R.id.price_text_view);\n" +
" notificationManager = NotificationManagerCompat.from(this);\n\n" +
" handler = new Handler();\n" +
" runnable = new Runnable() {\n" +
" @Override\n" +
" public void run() {\n" +
" // 每五秒钟更新价格\n" +
" double price = Math.round(Math.random() * 10000.0) / 100.0;\n" +
" priceTextView.setText(\"$\" + price);\n\n" +
" // 显示通知\n" +
" String code = \"public class MainActivity {\\n\" +\n" +
" \" private TextView priceTextView;\\n\" +\n" +
" \" private Handler handler;\\n\" +\n" +
" \" private Runnable runnable;\\n\" +\n" +
" \" private NotificationManagerCompat notificationManager;\\n\\n\" +\n" +
" \" @Override\\n\" +\n" +
" \" protected void onCreate(Bundle savedInstanceState) {\\n\" +\n" +
" \" super.onCreate(savedInstanceState);\\n\" +\n" +
" \" setContentView(R.layout.activity_main);\\n\\n\" +\n" +
" \" priceTextView = findViewById(R.id.price_text_view);\\n\" +\n" +
" \" notificationManager = NotificationManagerCompat.from(this);\\n\\n\" +\n" +
" \" handler = new Handler();\\n\" +\n" +
" \" runnable = new Runnable() {\\n\" +\n" +
" \" @Override\\n\" +\n" +
" \" public void run() {\\n\" +\n" +
" \" // 每五秒钟更新价格\\n\" +\n" +
" \" double price = Math.round(Math.random() * 10000.0) / 100.0;\\n\" +\n" +
" \" priceTextView.setText(\\\"$\\\" + price);\\n\\n\" +\n" +
" \" // 显示通知\\n\" +\n" +
" \" NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)\\n\" +\n" +
" \" .setSmallIcon(R.drawable.ic_notifications)\\n\" +\n" +
" \" .setContentTitle(\\\"Android Studio Price Update\\\")\\n\" +\n" +
" \" .setContentText(\\\"The price has been updated to $\\\" + price)\\n\" +\n" +
" \" .setPriority(NotificationCompat.PRIORITY_DEFAULT)\\n\" +\n" +
" \" .setAutoCancel(true);\\n\\n\" +\n" +
" \" notificationManager.notify(NOTIFICATION_ID, builder.build());\\n\" +\n" +
" \" handler.postDelayed(this, 5000);\\n\" +\n" +
" \" }\\n\" +\n" +
" \" };\\n\" +\n" +
" \" handler.postDelayed(runnable, 5000);\\n\" +\n" +
" \" }\\n\" +\n" +
" \"}\";\n\n" +
" NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)\n" +
" .setSmallIcon(R.drawable.ic_notifications)\n" +
" .setContentTitle(\"Android Studio Price Update\")\n" +
" .setContentText(\"The price has been updated to $\" + price)\n" +
" .setPriority(NotificationCompat.PRIORITY_DEFAULT)\n" +
" .setAutoCancel(true);\n\n" +
" notificationManager.notify(NOTIFICATION_ID, builder.build());\n" +
" handler.postDelayed(this, 5000);\n" +
" }\n" +
" };\n" +
" handler.postDelayed(runnable, 5000);\n" +
" }\n" +
"}";
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notifications)
.setContentTitle("Android Studio Price Update")
.setContentText("The price has been updated to $" + price)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
notificationManager.notify(NOTIFICATION_ID, builder.build());
handler.postDelayed(this, 5000);
}
};
handler.postDelayed(runnable, 5000);
}
// 创建通知渠道
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(CHANNEL_DESCRIPTION);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
private static final String CHANNEL_ID = "androidstudio_price_update";
private static final String CHANNEL_NAME = "Android Studio Price Update";
private static final String CHANNEL_DESCRIPTION = "Show updates of Android Studio price";
private static final int NOTIFICATION_ID = 1;
}
```
在此示例中,我们使用Handler和Runnable来实现每5秒钟更新价格的功能,并使用NotificationCompat.Builder来显示通知。在通知中,我们还包含了完整的代码。请注意,我们还需要在AndroidManifest.xml文件中添加通知权限:
```xml
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
...
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
```
在此示例中,我们还创建了一个通知渠道,并将其添加到通知管理器中。我们还可以在BroadcastReceiver中使用此示例代码,以便在设备启动时自动启动应用程序并开始更新价格。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)