在关闭应用程序时打开/关闭屏幕崩溃

时间:2017-04-28 08:37:22

标签: android android-service android-broadcastreceiver

当应用程序打开或关闭时,我正在检测屏幕开/关时间

使用服务和广播接收器。 当应用程序正在运行或最小化时它运行良好但在关闭应用程序时它会崩溃。

如果在没有崩溃的情况下关闭应用程序,如何获得屏幕开/关时间。

logcat错误

E/ActivityThread: Activity com.shah.neeraj.s.MainActivity has leaked IntentReceiver com.shah.neeraj.s.ScreeReceiver@8686ff5 that was originally registered here. Are you missing a call to unregisterReceiver()?
                  android.app.IntentReceiverLeaked: Activity com.shah.neeraj.s.MainActivity has leaked IntentReceiver com.shah.neeraj.s.ScreeReceiver@8686ff5 that was originally registered here. Are you missing a call to unregisterReceiver()?
                      at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1159)
                      at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:946)
                      at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1330)
                      at android.app.ContextImpl.registerReceiver(ContextImpl.java:1310)
                      at android.app.ContextImpl.registerReceiver(ContextImpl.java:1304)
                      at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:586)
                      at com.shah.neeraj.s.MainActivity.onCreate(MainActivity.java:37)
                      at android.app.Activity.performCreate(Activity.java:6672)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6123)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Application terminated.

MainActivity

public class MainActivity extends Activity {
    TextView text1,text2,text3;   
    Date d,d1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        text1 = (TextView) findViewById(R.id.text1);
        text2 = (TextView) findViewById(R.id.text2);
        text3 =(TextView) findViewById(R.id.text3);    

        // INITIALIZE RECEIVER
       IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreeReceiver();
        registerReceiver(mReceiver, filter);
//        unregisterReceiver(mReceiver);
        PowerManager pm =(PowerManager) getSystemService(Context.POWER_SERVICE);
        // YOUR CODE
    }

    @Override
    protected void onPause() {
        // WHEN THE SCREEN IS ABOUT TO TURN OFF
        if (ScreeReceiver.wasScreenOn)
        {
        //if (pm.isScreenOn() == false){
            // THIS IS THE CASE WHEN ONPAUSE() IS CALLED BY THE SYSTEM DUE TO A SCREEN STATE CHANGE

            d = new Date();
            CharSequence s = DateFormat.format("hh:mm:ss", d.getTime());

            System.out.println("SCREEN TURNED OFF");
            text1.setText("SCREEN TURNED OFF" + s);
            Log.i("hi", "s"+ s);  
        } else {
            // THIS IS WHEN ONPAUSE() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
        }
        super.onPause();    
    }

    @Override
    protected void onResume() {
        // ONLY WHEN SCREEN TURNS ON
        if (!ScreeReceiver.wasScreenOn) {
            // THIS IS WHEN ONRESUME() IS CALLED DUE TO A SCREEN STATE CHANGE

            d1 = new Date();
            CharSequence s1 = DateFormat.format("hh:mm:ss", d1.getTime());

            text2.setText("SCREEN TURNED ON" + s1);
            Log.i("hi", "s"+ s1);

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

            Timer updateTimer = new Timer();
            updateTimer.schedule(new TimerTask()
            {    
                public void run()
                {
                    try
                    {    
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {    
                                //stuff that updates ui
                                long diff = d1.getTime() - d.getTime();

                                int Hours = (int) (diff/( 1000* 60 * 60));
                                int Mins = (int) (diff/(1000*60)) % 60;

                                Log.i("Difference ", ""+ diff);

                                String diff1= Hours + ":" + Mins; // updated value every 60 second
                                text3.setText( diff1 );
                                Log.i("Differ", "diff"+ diff1);
                            }
                        });    
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }    
            }, 0, 600000);    
        } else {
        }
        super.onResume();  
    }    

    @Override
    public void onDestroy() {
        super.onDestroy();
        //Log.d(msg, "The onDestroy() event");
        /*IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreeReceiver();
        unregisterReceiver(mReceive);*/    
    }
}

ScreenReceiver

public class ScreeReceiver extends BroadcastReceiver {
    public static boolean wasScreenOn = true;
    @Override
    public void onReceive(Context context, Intent intent) {
        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;
        }
        Intent i = new Intent(context, UpdateService.class);
        i.putExtra("screen_state", wasScreenOn);
        context.startService(i);

        Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
    }    
}

UpdateService

public class UpdateService extends Service {
    TextView text1,text2,text3;
    @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);
        BroadcastReceiver mReceiver = new ScreeReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {
           // YOUR CODE
            /* Date d = new Date();
            CharSequence s = DateFormat.format("hh:mm:ss", d.getTime());

            System.out.println("SCREEN TURNED OFF");
            text1.setText("SCREEN TURNED OFF" + s);
            Log.i("hi", "s"+ s);
            */
            Date d = new Date();
            CharSequence s = DateFormat.format("hh:mm:ss", d.getTime());

            System.out.println("SCREEN TURNED OFF");
            Log.i("hi Service t off", "s"+ s);

           // text1.setText("SCREEN TURNED OFF" + s);
            System.out.println("SCREEN  Off"+s);

        } else {
            // YOUR CODE
            Date  d1 = new Date();
            CharSequence s1 = DateFormat.format("hh:mm:ss", d1.getTime());

            //text2.setText("SCREEN TURNED ON" + s1);
            Log.i("hi Service t on ", "s"+ s1);

            System.out.println("SCREEN  ON"+s1);
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onDestroy() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreeReceiver();
        unregisterReceiver(mReceiver);
    }

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shah.neeraj.s">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">

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

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


        </activity>

       <receiver android:name="ScreeReceiver">

           <intent-filter>
               <action android:name="android.intent.action.SCREEN_OFF"/>
               <action android:name="android.intent.action.SCREEN_ON"/>
           </intent-filter>
        </receiver>



        <service android:name=".UpdateService">

        </service>
    </application>

</manifest>

3 个答案:

答案 0 :(得分:3)

你的Logcat返回什么

  

最初在这里注册。你错过了一个电话吗?   unregisterReceiver()?

首先,宣布 BroadcastReceiver 为全球&amp;您应该在 onPause() 方法

中拨打以下电话

然后打开 onCreate() 部分。

unregisterReceiver(mReceive);

<强>最后

 @Override
protected void onStop()
{    ........................
    unregisterReceiver(mReceive);
    super.onStop();
}

答案 1 :(得分:0)

您正在注册接收方但未注销接收方。

您应该分别在onResume()和onPause()(或onStart()和onStop())中注册和注销Receiver。

答案 2 :(得分:-1)

取消注释,

BroadcastReceiver mReceiver = new ScreeReceiver();
    unregisterReceiver(mReceive);

错误表明您没有取消注册接收器。