编辑
现在代码工作(感谢olgun)但媒体播放器播放并没有停止,每次打开/关闭屏幕都会创建新的媒体播放器对象(多次播放).
SCbroadcaster.java
public class SCbroadcaster extends broadcastReceiver { PowerManager pm; MediaPlayer mp; public static boolean wasScreenOn = true; @OverrIDe public voID onReceive(Context arg0,Intent arg1) { // Todo auto-generated method stub mp = MediaPlayer.create(arg0,R.raw.gale); if (arg1.getAction().equals(Intent.ACTION_SCREEN_ON)) { mp.stop(); mp.release(); Toast.makeText(arg0,"oNONONO",Toast.LENGTH_LONG).show(); } else if (arg1.getAction().equals(Intent.ACTION_SCREEN_OFF)) { mp.start(); } }}
MainActivity.java
public class MainActivity extends Activity {@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); broadcastReceiver bre = new SCbroadcaster(); registerReceiver(bre,filter);}protected voID onPause() { super.onPause(); if (SCbroadcaster.wasScreenOn) { System.out.println("SCREEN TURNED OFF"); } else { }}protected voID onResume() { super.onResume(); if (!SCbroadcaster.wasScreenOn) { } else { }}
}
解决方法 Intent.ACTION_SCREEN_OFF和ACTION_SCREEN_ON签出上述广播注册. here你可以找到一个很好的例子. 总结以上是内存溢出为你收集整理的android – 屏幕开/关检测全部内容,希望文章能够帮你解决android – 屏幕开/关检测所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)