如何将我的android studio项目设置为我的手机背景?

ybzsozfc  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(367)

我已经创建了一个android工作室项目,其中包含一个直到圣诞节的倒计时。我使用两个图像来创建倒计时计时器的背景和轮廓。我已经使该应用程序看起来不错,我想知道是否有任何办法,以设置为我的手机的背景项目,如果它是可能的,如何做到这一点。下面的代码是我如何设置计时器并给出它的值。我有什么需要改变的吗?

  1. package com.example.holidaycountdown;
  2. import android.app.WallpaperManager;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11. import androidx.appcompat.app.AppCompatActivity;
  12. import java.io.IOException;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Date;
  15. public class MainActivity extends AppCompatActivity {
  16. private TextView txtTimerDay, txtTimerHour, txtTimerMinute, txtTimerSecond;
  17. private TextView tvEvent;
  18. private Handler handler;
  19. private Runnable runnable;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. txtTimerDay = (TextView) findViewById(R.id.txtTimerDay);
  25. txtTimerHour = (TextView) findViewById(R.id.txtTimerHour);
  26. txtTimerMinute = (TextView) findViewById(R.id.txtTimerMinute);
  27. txtTimerSecond = (TextView) findViewById(R.id.txtTimerSecond);
  28. tvEvent = (TextView) findViewById(R.id.tvhappyevent);
  29. countDownStart();
  30. Button button = (Button) findViewById(R.id.button);
  31. button.setOnClickListener(new View.OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. setWallpaper();
  35. }
  36. });
  37. }
  38. private void setWallpaper() {
  39. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper);
  40. WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
  41. try{
  42. manager.setBitmap(bitmap);
  43. Toast.makeText(this, "Wallpaper set!", Toast.LENGTH_SHORT).show();
  44. } catch (IOException e) {
  45. Toast.makeText(this, "Error!", Toast.LENGTH_SHORT).show();
  46. }
  47. }
  48. public void countDownStart() {
  49. handler = new Handler();
  50. runnable = new Runnable() {
  51. @Override
  52. public void run() {
  53. handler.postDelayed(this, 1000);
  54. try {
  55. SimpleDateFormat dateFormat = new SimpleDateFormat(
  56. "yyyy-MM-dd");
  57. // Please here set your event date//YYYY-MM-DD
  58. Date futureDate = dateFormat.parse("2020-12-25");
  59. Date currentDate = new Date();
  60. if (!currentDate.after(futureDate)) {
  61. long diff = futureDate.getTime()
  62. - currentDate.getTime();
  63. long days = diff / (24 * 60 * 60 * 1000);
  64. diff -= days * (24 * 60 * 60 * 1000);
  65. long hours = diff / (60 * 60 * 1000);
  66. diff -= hours * (60 * 60 * 1000);
  67. long minutes = diff / (60 * 1000);
  68. diff -= minutes * (60 * 1000);
  69. long seconds = diff / 1000;
  70. txtTimerDay.setText("" + String.format("%02d", days));
  71. txtTimerHour.setText("" + String.format("%02d", hours));
  72. txtTimerMinute.setText(""
  73. + String.format("%02d", minutes));
  74. txtTimerSecond.setText(""
  75. + String.format("%02d", seconds));
  76. } else {
  77. tvEvent.setVisibility(View.VISIBLE);
  78. tvEvent.setText("The event started!");
  79. textViewGone();
  80. }
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. };
  86. handler.postDelayed(runnable, 1 * 1000);
  87. }
  88. public void textViewGone() {
  89. findViewById(R.id.LinearLayout10).setVisibility(View.GONE);
  90. findViewById(R.id.LinearLayout11).setVisibility(View.GONE);
  91. findViewById(R.id.LinearLayout12).setVisibility(View.GONE);
  92. findViewById(R.id.LinearLayout13).setVisibility(View.GONE);
  93. findViewById(R.id.textView1).setVisibility(View.GONE);
  94. findViewById(R.id.textView2).setVisibility(View.GONE);
  95. }
  96. }
js4nwp54

js4nwp541#

似乎您需要为您的应用程序创建一个应用程序小部件(应用程序小部件是可以嵌入到其他应用程序(如主屏幕)中并接收定期更新的微型应用程序视图)。
这里有一个有用的教程
谷歌教程

相关问题