当服务启动活动时,Android全屏不起作用

时间:2012-05-30 20:10:09

标签: android service android-activity fullscreen

所以我正在开发一个替代的lockscreenapp 我需要阻止点击通知栏

所以我尝试在fullscreenn中运行这个应用程序

但是当它被服务调用时,通知栏仍然是它们在启动器调用时它会变得不可见 码: 公共类应用扩展了活动{

TextView datum;
static TextView time;

static TextView temperature;
static ImageView weather_icon;
static TextView weather_refreshed;
static RelativeLayout refresh;

private static Context mContext;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);



    this.startService(new Intent(this, lockservice.class));


    mContext = this;


     //Date
     datum = (TextView)  findViewById(R.id.datum);
     SimpleDateFormat sdf = new SimpleDateFormat("E dd MMM");
     String currentDateandTime = sdf.format(new Date());
     datum.setText(currentDateandTime);

     //Time
     time = (TextView)  findViewById(R.id.clock);
     Date date = new Date();
     int hours = date.getHours();
     String diplay_hours = String.valueOf(hours);
     int minutes = date.getMinutes();
     String diplay_minutes = String.valueOf(minutes);
     if(hours < 10)
     {
         diplay_hours = "0"+String.valueOf(hours); 
     }
     if(minutes < 10)
     {
         diplay_minutes = "0"+String.valueOf(minutes); 
     }
     time.setText(String.valueOf(diplay_hours+":"+diplay_minutes));


     //update
     Thread myThread = new Thread(new UpdateThread());
     myThread.start();

     // Weather
     temperature = (TextView)  findViewById(R.id.weather_temp);
     weather_icon = (ImageView)  findViewById(R.id.weather_icon);
     weather_refreshed = (TextView)  findViewById(R.id.weather_refreshed);



     refresh = (RelativeLayout)  findViewById(R.id.relativeLayout1);
     refresh.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            weather.download();

        }
        });
}
@Override
public void onResume() {
    super.onResume();


}



@Override 
public void onAttachedToWindow()
{  
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);




    super.onAttachedToWindow();  

}
@Override 
public boolean onKeyDown(int iKeyCode, KeyEvent event)
{

    if(iKeyCode == KeyEvent.KEYCODE_BACK || iKeyCode == KeyEvent.KEYCODE_HOME) 
    {
        finish();
        return true;
    }
    return false;
   }


//update
public Handler updateHandler = new Handler(){
    /** Gets called on every message that is received */
    // @Override
    public void handleMessage(Message msg) {

        //Time
     time = (TextView)  findViewById(R.id.clock);
     Date date = new Date();
     int hours = date.getHours();
     String diplay_hours = String.valueOf(hours);
     int minutes = date.getMinutes();
     String diplay_minutes = String.valueOf(minutes);
     if(hours < 10)
     {
         diplay_hours = "0"+String.valueOf(hours); 
     }
     if(minutes < 10)
     {
         diplay_minutes = "0"+String.valueOf(minutes); 
     }
     time.setText(String.valueOf(diplay_hours+":"+diplay_minutes));


     //weather
     weather.display();
     try
     {
        SharedPreferences weather = app.getContext().getSharedPreferences("weather",app.getContext().MODE_WORLD_READABLE);
    app.weather_refreshed.setText(weather.getString("time",""));
     }
     catch(Exception x)
     {

     }
    super.handleMessage(msg);
    }
};


public class UpdateThread implements Runnable {



    @Override
    public void run() {
        while(true){

            long startTime = System.currentTimeMillis();
            app.this.updateHandler.sendEmptyMessage(0); //handler

            Thread.yield();                 
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

}

public static Context getContext(){
    return mContext;
}

 @Override
    public void onPause() {

        super.onPause();

    }

我也使用Keyguard 全屏在我的清单中实现:

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".app"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> 
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="boot">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>

        <service
        android:name="lockservice" 
        android:process=":lockscreen" 
        android:icon="@drawable/ic_launcher"
        android:label="Lockscreen">
        </service>

        <activity
            android:name=".weather_update"
            android:theme="@android:style/Theme.Translucent">

        </activity>

    </application>

2 个答案:

答案 0 :(得分:0)

我在使用之前使用了全屏,并遇到了一些问题,但没有尝试从服务启动活动,尝试以这种方式应用全屏,

WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);

万一你需要退出全屏,你可以这样做,

WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(attrs);
试一试,祝你好运!

答案 1 :(得分:0)

似乎this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);导致问题。

其他人建议推迟通话:请参阅Activity doesn't show in full screen