我想在屏幕上显示消息打开或关闭。我已经创建了一个广播接收器和一个服务,如上:
广播接收器代码:
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>
我已经运行了我的代码,但是每当我打开/关闭屏幕时,屏幕上都没有任何消息。如何解决它?