Android 设备屏幕关闭时如何设置闹钟?

发布于 2024-12-28 23:44:15 字数 4241 浏览 1 评论 0原文

您好,我想设置手机屏幕关闭时的警报通知。我的代码是'

 package com.my.timeout;
 import java.util.Calendar;
 import android.app.Activity;
 import android.app.AlarmManager;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.Bundle;
 import android.widget.Toast;
 public class ScreenON_OFF_ACTIVITY extends Activity
 {
 AlarmManager am;
  private static final int REQUEST_CODE = 0;

@Override
  public void onCreate(Bundle savedInstanceState) 
  {
         super.onCreate(savedInstanceState);

         onCreate1();

  }

  public void onCreate1() {



         // initialize receiver

     System.out.println("onCreate1 ");

         IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

         filter.addAction(Intent.ACTION_SCREEN_OFF);

         BroadcastReceiver mReceiver = new ScreenReceiver();

         registerReceiver(mReceiver, filter);

         System.out.println("onCreate ");
         }


     @Override

     protected void onPause() {

         // when the screen is about to turn off

         if (ScreenReceiver.screenOff) {

             // this is the case when onPause() is called by the system due to a screen state change

             System.out.println("SCREEN TURNED OFF");

         } else {

             // this is when onPause() is called when the screen state has not changed
      System.out.println("this is when onPause() is called when the screen state has not changed ");
     display_alarm();
         }
       super.onPause();

    }

     @Override

     protected void onResume() {

         // only when screen turns on

         if (!ScreenReceiver.screenOff) {

             // this is when onResume() is called due to a screen state change

             System.out.println("SCREEN TURNED ON");

         } else {

             // this is when onResume() is called when the screen state has not changed

           System.out.println(" this is when onResume() is called when the screen state has not changed ");

         }

         super.onResume();

     }
 }

ScreenReceiver.java

     package com.my.timeout;

    import android.content.BroadcastReceiver;
   import android.content.Context;
    import android.content.Intent;

    public class ScreenReceiver extends BroadcastReceiver
       {     

    public static boolean screenOff;



    @Override

    public void onReceive(Context context, Intent intent) {



      System.out.println("onReceive ");

        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

            screenOff = true;

            System.out.println("SCREEN TURNED OFF on BroadcastReceiver");

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

            screenOff = false;

            System.out.println("SCREEN TURNED ON on BroadcastReceiver");

        }

        Intent i = new Intent(context, UpdateService.class);

        i.putExtra("screen_state", screenOff);

        context.startService(i);

    }



}

UpdateService.java

   package com.my.timeout;

   import android.app.Service;
    import android.content.BroadcastReceiver;
   import android.content.Intent;
   import android.content.IntentFilter;
    import android.os.IBinder;

     public class UpdateService extends Service {


    public void onCreate() {

        super.onCreate();

        // register receiver that handles screen on and screen off logic



        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

        filter.addAction(Intent.ACTION_SCREEN_OFF);

        BroadcastReceiver mReceiver = new ScreenReceiver();

        registerReceiver(mReceiver, filter);
    }



    @Override

    public void onStart(Intent intent, int startId) {

        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {

         System.out.println("Screen is off");

        } else {

            System.out.println("Screen is on");

        }

    }



 @Override

 public IBinder onBind(Intent intent) {

 return null;

 }

}

现在我想在屏幕关闭时设置警报。我该怎么做。有人可以告诉我吗?提前致谢。

Hi I want to set alarm notification when the mobile screen is off. My code is'

 package com.my.timeout;
 import java.util.Calendar;
 import android.app.Activity;
 import android.app.AlarmManager;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.Bundle;
 import android.widget.Toast;
 public class ScreenON_OFF_ACTIVITY extends Activity
 {
 AlarmManager am;
  private static final int REQUEST_CODE = 0;

@Override
  public void onCreate(Bundle savedInstanceState) 
  {
         super.onCreate(savedInstanceState);

         onCreate1();

  }

  public void onCreate1() {



         // initialize receiver

     System.out.println("onCreate1 ");

         IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

         filter.addAction(Intent.ACTION_SCREEN_OFF);

         BroadcastReceiver mReceiver = new ScreenReceiver();

         registerReceiver(mReceiver, filter);

         System.out.println("onCreate ");
         }


     @Override

     protected void onPause() {

         // when the screen is about to turn off

         if (ScreenReceiver.screenOff) {

             // this is the case when onPause() is called by the system due to a screen state change

             System.out.println("SCREEN TURNED OFF");

         } else {

             // this is when onPause() is called when the screen state has not changed
      System.out.println("this is when onPause() is called when the screen state has not changed ");
     display_alarm();
         }
       super.onPause();

    }

     @Override

     protected void onResume() {

         // only when screen turns on

         if (!ScreenReceiver.screenOff) {

             // this is when onResume() is called due to a screen state change

             System.out.println("SCREEN TURNED ON");

         } else {

             // this is when onResume() is called when the screen state has not changed

           System.out.println(" this is when onResume() is called when the screen state has not changed ");

         }

         super.onResume();

     }
 }

ScreenReceiver.java

     package com.my.timeout;

    import android.content.BroadcastReceiver;
   import android.content.Context;
    import android.content.Intent;

    public class ScreenReceiver extends BroadcastReceiver
       {     

    public static boolean screenOff;



    @Override

    public void onReceive(Context context, Intent intent) {



      System.out.println("onReceive ");

        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

            screenOff = true;

            System.out.println("SCREEN TURNED OFF on BroadcastReceiver");

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

            screenOff = false;

            System.out.println("SCREEN TURNED ON on BroadcastReceiver");

        }

        Intent i = new Intent(context, UpdateService.class);

        i.putExtra("screen_state", screenOff);

        context.startService(i);

    }



}

UpdateService.java

   package com.my.timeout;

   import android.app.Service;
    import android.content.BroadcastReceiver;
   import android.content.Intent;
   import android.content.IntentFilter;
    import android.os.IBinder;

     public class UpdateService extends Service {


    public void onCreate() {

        super.onCreate();

        // register receiver that handles screen on and screen off logic



        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

        filter.addAction(Intent.ACTION_SCREEN_OFF);

        BroadcastReceiver mReceiver = new ScreenReceiver();

        registerReceiver(mReceiver, filter);
    }



    @Override

    public void onStart(Intent intent, int startId) {

        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {

         System.out.println("Screen is off");

        } else {

            System.out.println("Screen is on");

        }

    }



 @Override

 public IBinder onBind(Intent intent) {

 return null;

 }

}

Now i want to set alarm when the screen has been go off. How can i do this.Can anybody tell me? thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文