2011-09-09 20 views
7

Tôi đang cố gắng tắt WiFi khi màn hình TẮT (bị khóa) và bật lại khi màn hình BẬT (mở khóa).ACTION_SCREEN_ON và ACTION_SCREEN_OFF không hoạt động?

Tôi đã thực hiện BroadcastReceiver; đưa vào manifest mã này:

<receiver android:name="MyIntentReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       <action android:name="android.intent.action.SCREEN_OFF" /> 
       <action android:name="android.intent.action.SCREEN_ON" /> 
       <action android:name="android.intent.action.USER_PRESENT" /> 
       <category android:name="android.intent.category.HOME" /> 
       <category android:name="android.intent.category.LAUNCHER" />  
      </intent-filter> 
     </receiver> 

và đây là lớp MyIntentReceiver:

package org.androidpeople.boot; 

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

public class MyIntentReceiver extends BroadcastReceiver { 
    // Called when boot completes 

    public static boolean startup; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Set what activity should launch after boot completes 

     System.out.println("Intent Action: " + intent.getAction()); 

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

      System.out.println("locked : ACTION_SCREEN_OFF"); 

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

      System.out.println("not locked : ACTION_SCREEN_ON "); 

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

      System.out.println("User Unlocking it "); 

     } 
     else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 
      // this to indicate that program is running 
      // automaticlly not manually by user 
      startup = true; 
      System.out.println("Automatic BOOT at StartUp"); 

      Intent startupBootIntent = new Intent(context, LaunchActivity.class); 
      startupBootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(startupBootIntent); 
     } 

    } 
} 

Và kết quả là - cả ACTION_SCREEN_ONACTION_SCREEN_OFF không bao giờ bị sa thải! USER_PRESENTBOOT_COMPLETED hoạt động tốt nhưng người kia thì không. Tôi đang sử dụng một trình giả lập, không phải là một thiết bị thực - điều này có thể gây ra sự cố không?

Bất kỳ trợ giúp nào? Tôi cần bật và tắt màn hình để bật/tắt WiFi để tiết kiệm pin.

Cảm ơn trước

+0

http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/ –

Trả lời

8

Bạn không thể nắm bắt những ý định đó thông qua XML (Tôi quên lý do). Tuy nhiên, bạn có thể sử dụng số Service đăng ký thành viên BroadcastReceiver trong số onStartCommand() và hủy đăng ký số này trong số onDestroy(). Điều này sẽ yêu cầu dịch vụ được chạy trong nền, liên tục hoặc miễn là bạn cần nó, vì vậy hãy chắc chắn để khám phá các tuyến đường thay thế.

Bạn có thể xác định BroadcastReceiver trong lớp Service bạn như vậy:

private final class ScreenReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(final Context context, final Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
      //stuff 
     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
      //other stuff 
     } 
    } 
} 

Đối với một ví dụ hơi phức tạp, nhưng một trong đó cho thấy cách BroadcastReceiverService tương tác thấy CheckForScreenBugAccelerometerService từ ứng dụng của tôi, ElectricSleep.

12

Để chụp các hành động SCREEN_OFF và SCREEN_ON (và có thể là các hành động khác), bạn phải định cấu hình BroadcastReceiver theo mã, không phải thông qua tệp kê khai.

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
intentFilter.addAction(Intent.ACTION_SCREEN_OFF); 
BroadcastReceiver mReceiver = new ScreenStateBroadcastReceiver(); 
registerReceiver(mReceiver, intentFilter); 

Đã kiểm tra và hoạt động bình thường.

+0

+1, cho IntentFilter bạn sử dụng BẬT và cho hành động TẮT? –

+0

Cả hai đều là các hành động được đặt thành IntentFilter. – PoOk

+0

Tôi đã thêm theo cách này nhưng khi tôi xóa ứng dụng gần đây thì phương thức onReceive của BroadcastReceiver không phản hồi. Nếu ứng dụng chạy thì onReceive hoạt động tốt. –

Các vấn đề liên quan