QQ音乐锁屏实现

不需要权限

讲解部分:
http://blog.csdn.net/working_harder/article/details/52629919

代码实现

Mainnifest.xml





    
        
            
                

                
            
        

        

        
        

        

        

    


主题 style.xml



    
    


    


    



Service

package com.ruulai.tools;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

    private BroadcastReceiver broadcastReceiver;

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


    @Override
    public void onCreate() {
        super.onCreate();
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();

                Toast.makeText(MyService.this, "收到锁屏消息", Toast.LENGTH_SHORT).show();


                if (action.equals(Intent.ACTION_SCREEN_OFF)) {

                    Intent lockscreen = new Intent(MyService.this, LockScreenActivity.class);

                    lockscreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // ------->

                    startActivity(lockscreen);
                    Toast.makeText(MyService.this, "启动锁屏Activity", Toast.LENGTH_SHORT).show();

                }
            }
        };

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(broadcastReceiver, intentFilter);



    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.d("MyService", "Service Start");

        return START_NOT_STICKY;
    }
}


锁屏Activity

package com.ruulai.tools;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

public class LockScreenActivity extends AppCompatActivity{


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

        setContentView(R.layout.lock);

    }

    @Override
    public void onBackPressed() {
//        锁屏界面当然不响应Back按键, 只需要重写Activity的onBackPressed方法即可


    }

    public void test(View view) {

        Toast.makeText(this, "你点击了Test", Toast.LENGTH_SHORT).show();


    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

    }
}


MainActivity

package com.ruulai.tools;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }


    public void open(View view) {

        Intent intent = new Intent();
        intent.setClass(this, MyService.class);
        startService(intent);

    }

}

你可能感兴趣的:(QQ音乐锁屏实现)