2014-03-26 210 views
0

我想在重新引導之前甚至在屏幕鎖定之前開始一項活動。這裏是我的代碼如何在屏幕鎖定顯示之前調用活動?

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.bootservicestartup.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

    <receiver android:name=".BootUpReceiver"> 

     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <action android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 

    </receiver> 


</application> 

public class BootUpReceiver extends BroadcastReceiver { 

@SuppressLint("InlinedApi") 
@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 

    Intent i = new Intent(context, MainActivity.class); 
    i.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(i); 

} 

代碼的功能是當手機重新啓動時,會顯示一個活動。這樣可行。但我也希望在顯示屏幕鎖定之前顯示活動。

回答

0

希望這可以幫助你,如果你做它的服務;

你需要在你AndroidManifest.xml文件中的以下內容:

1)在你的元素:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

2)在你的元素(一定要使用完全合格的[或相對]類名爲您的廣播接收器):

<receiver android:name="com.example.MyBroadcastReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 

(你不需要在android:啓用,出口等,屬性... Android的默認值是正確的)

在MyBroadcastReceiver.java:

package com.example; 

public class MyBroadcastReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent startServiceIntent = new Intent(context, MyService.class); 
     context.startService(startServiceIntent); 
    } 
} 
+0

問題鎖屏後,活動啓動。我希望在查看屏幕鎖定之前顯示活動? – user3457807

+0

服務工作,如果屏幕鎖定或不,但您可以添加此標籤到您的佈局 android:keepScreenOn =「true」 或 getWindow()。addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 這將授予屏幕不會鎖定 – AbuQauod

相關問題