当收到gcm消息时,唤醒锁定屏幕在android 4.1.3中不起作用

时间:2014-03-25 19:49:03

标签: android google-cloud-messaging wakelock

我们正在开发Android应用。 当我们的应用程序在睡眠模式下收到GCM消息时,我们将唤醒锁定模式。

程序在Android 4.1(Jelly-Bean,API 16)中运行良好。 但它在Android 4.3(Jelly-Bean,API 19)中不起作用。 问题是什么?请告诉我们!!!

我们编程如下:

以下是类文件:

public class GCMIntentService extends GCMBaseIntentService {
@Override
protected void onMessage(Context context, Intent intent) {
    //...some code omitted...

    Intent newIntent;
    newIntent = new Intent(context, SomeActivity.class);
    newIntent.setFlags(
            Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    context.startActivity(newIntent);
}
}


public class SomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    //...some code omitted...
    WakeLockManager.setWakeUp(this);
    //...some code omitted...
    super.onCreate(savedInstanceState);
}
@Override
protected void onStart() {
    //...some code omitted...
    super.onStart();
}   
@Override
protected void onResume() {
    //...some code omitted...
    super.onStart();    
}
@Override
protected void onPause() {
    //...some code omitted...
    super.onPause();
}

protected void onStop() {
    //...some code omitted...
    super.onStop();
};

@Override
protected void onDestroy() {
    //...some code omitted...
    super.onDestroy();
}   
}


public class WakeLockManager {
public static void setWakeUp(Context context) {
    Window wnd = ((Activity)context).getWindow();
    wnd.addFlags(
        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
        WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        );
}
}

以下是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="19" />

<permission
    android:name="com.mycompany.myapp.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
    <uses-permission android:name="com.mycompany.myapp.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>       
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE"/>    

<application
    android:name="com.mycompany.myapp"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppBaseTheme" >

    <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.mycompany.myapp" />
        </intent-filter>
    </receiver>

    <service android:name="com.mycompany.myapp.GCMIntentService" /> 

    <activity
        android:name="com.mycompany.myapp.activities.SomeActivity"
        android:configChanges="orientation"
        android:label="@string/app_name"
        android:theme="@style/NoActionBar"
        android:screenOrientation="landscape" >
    </activity>

    <activity
        android:name="com.appkorea.newdriver.activities.MainActivity"
        android:configChanges="orientation"
        android:label="@string/app_name"
        android:theme="@style/AuthTheme"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>                
    </activity>     

    <!-- ...some code omitted... -->          

</application>

</manifest>

在API 19的情况下,当注意SomeActivity时,onCreate()回调函数被调用超过1次。 我想有几个原因如下。

1. SomeActivity's screenOrientation value is landscape. 
   So for rotating activity, the onCreate() call-back function is called once.
2. To unlock the lockScreen, the android system show the lock screen, 
   and rotate the screen into portrait. 
   because current lock-screen's screenOrientation value is portrait. 
   At this time, SomeActivity is already created. 
   so the onCreate call-back function is called once.
3. After unlocking, To show the SomeActivity, the onCreate() is called once. 
   but now device screenOrientation is portrait, 
   and SomeActivity's screenOrientation is landscape. 
   so the onCreate() is called again.

如上所述,onCreate()被调用超过1次。不幸的是,我现在没有安装Android手机4.3。所以我无法显示logcat。稍后,我将上传logcat屏幕截图。真的感谢阅读。

0 个答案:

没有答案