2012-12-20 129 views
2

我想在屏幕上顯示消息開啓或關閉。我創建了一個廣播接收器和如上一個服務:處理屏幕關閉和屏幕開啓意圖

代碼爲廣播接收器:

public class ScreenReceiver extends BroadcastReceiver { 


private boolean screenOff; 


@Override 
public void onReceive(final Context context, Intent intent) 
{ 
    if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
    { 
     screenOff = true; 
     //wasScreenOn = false; 

     Log.v("$$$$$$", "In Method: ACTION_SCREEN_OFF"); 
     Handler handler = new Handler() 
     { 
      @Override 
      public void handleMessage(Message msg) 
      { 
       Toast.makeText(context, "OFF", Toast.LENGTH_SHORT).show(); 
      } 
     }; 
     System.out.println("off"); 
     // context.startService(new Intent(context,UpdateService.class)); 
    } 
    else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
    { 
     screenOff = false; 
     // wasScreenOn = true; 

     Log.v("$$$$$$", "In Method: ACTION_SCREEN_ON"); 
     //context.startService(new Intent(context,UpdateService.class)); 
     Handler handler = new Handler() 
     { 
      @Override 
      public void handleMessage(Message msg) 
      { 
       Toast.makeText(context, "ON", Toast.LENGTH_SHORT).show(); 
      } 
     }; 
     System.out.println("off"); 
    } 
    else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) 
    { 
     Log.v("$$$$$$", "In Method: ACTION_USER_PRESENT"); 
    } 
    Intent i = new Intent(context, UpdateService.class); 
    i.putExtra("screen_state", screenOff); 
    context.startService(i); 

    /* if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
     // do whatever you need to do here 
     wasScreenOn = false; 
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
     // and do whatever you need to do here 
     wasScreenOn = true; 
    }*/ 
} 

} 




public class UpdateService extends Service 
{ 
private BroadcastReceiver mReceiver; 


@Override 
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); 
    filter.addAction(Intent.ACTION_USER_PRESENT); 
    mReceiver = new ScreenReceiver(); 
    registerReceiver(mReceiver, filter); 
} 

@Override 
public void onStart(Intent intent, int startId) 
{ 
    boolean screenOn = intent.getBooleanExtra("screen_state", false); 
    if (!screenOn) 
    { 
     Toast.makeText(getApplication(), "OFFserv", Toast.LENGTH_SHORT).show(); 
     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) 
     { 
     // your code 
     Handler handler = new Handler() 
     { 
      @Override 
      public void handleMessage(Message msg) 
      { 
       Toast.makeText(getApplication(), "OFFserv", Toast.LENGTH_SHORT).show(); 
      } 
     }; 
     } 

    } else 
    { 
     if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
     { 
     // your code 
     Handler handler = new Handler() 
     { 
      @Override 
      public void handleMessage(Message msg) 
      { 
       Toast.makeText(getApplication(), "ONserv",Toast.LENGTH_SHORT).show(); 
      } 
     }; 
     } 
    } 
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) 
    { 
     Toast.makeText(getApplication(), "OFFserv", Toast.LENGTH_SHORT).show(); 
    // your code 
    Handler handler = new Handler() 
    { 
     @Override 
     public void handleMessage(Message msg) 
     { 
      Toast.makeText(getApplication(), "OFFserv", Toast.LENGTH_SHORT).show(); 
     } 
    }; 
    } 
    else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
     { 
     // your code 
     Handler handler = new Handler() 
     { 
      @Override 
      public void handleMessage(Message msg) 
      { 
       Toast.makeText(getApplication(), "ONserv", Toast.LENGTH_SHORT).show(); 
      } 
     }; 
     } 
} 

@Override 
public void onDestroy() 
{ 
     super.onDestroy(); 
     Log.v("$$$$$$", "In Method: onDestroy()"); 

     if (mReceiver != null) 
     { 
      unregisterReceiver(mReceiver); 
      mReceiver = null; 
     }   

} 

@Override 
public IBinder onBind(Intent intent) 
{ 
    // TODO Auto-generated method stub 
    return null; 
} 

} 

清單文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.screenreceiver" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="15" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".ExampleActivity" 

    > 
     <intent-filter > 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

<receiver android:name="com.example.screenreceiver.ScreenReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.SCREEN_ON" /> 
     <action android:name="android.intent.action.SCREEN_OFF" /> 
     <action android:name="android.Intent.ACTION_USER_PRESENT" /> 
    </intent-filter> 
</receiver> 
    <service android:name=".UpdateService"> 

    </service> 



</application> 

我有我的代碼運行,但有每當我打開/關閉我的屏幕時,屏幕上沒有任何信息出現。如何解決它?

+0

同樣的問題,仍然在尋找答案... – keybee

+0

hiii..keybee ...你的問題解決了或不? –

+0

不......看起來android並不是真的想聽那個。我可以理解它,因爲屏幕打開和關閉幾次,它不會有很多的應用程序都在聽這個,並放慢手機,特別是較弱的 - 更不用說擊球手的使用... – keybee

回答