如何在服务中实现onBackPressed()的功能?

时间:2016-05-30 18:28:32

标签: android service

我正在制作像My Home Button这样的应用。我已经实现了Home Button的功能,并且它正常工作。现在我被困在如何实现onBackPressed()和menuButton的功能,以便它将作为硬件backButton或menuButton被按下,并且当前的恢复活动onBackPressed()或menuButton函数将被调用?

这是我的代码 MainActivity.java

package com.example.chatheads;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    Button startService, stopService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService = (Button) findViewById(R.id.startService);
        stopService = (Button) findViewById(R.id.stopService);
        startService.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startService(new Intent(getApplication(), ChatHeadService.class));

            }
        });
        stopService.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                stopService(new Intent(getApplication(), ChatHeadService.class));

            }
        });
    }
}

ChatHeadService.java

    package com.example.chatheads;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

public class ChatHeadService extends Service {

    private WindowManager windowManager;
    private ImageView homeButton;
    private ImageView backButton;
    private ImageView menuButton;
    WindowManager.LayoutParams paramsHome;
    WindowManager.LayoutParams paramsBack;
    WindowManager.LayoutParams paramsMenu;

    @Override
    public void onCreate() {
        super.onCreate();

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        homeButton = new ImageView(this);
        backButton = new ImageView(this);
        menuButton = new ImageView(this);

        homeButton.setImageResource(R.drawable.face1);
        backButton.setImageResource(R.drawable.ic_launcher);
        menuButton.setImageResource(R.drawable.face1);

        paramsHome = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        paramsHome.gravity = Gravity.TOP | Gravity.LEFT;
        paramsHome.x = 0;
        paramsHome.y = 100;

        paramsBack = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        paramsBack.gravity = Gravity.TOP | Gravity.LEFT;
        paramsBack.x = 100;
        paramsBack.y = 100;

        paramsMenu = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        paramsMenu.gravity = Gravity.TOP | Gravity.LEFT;
        paramsMenu.x = 200;
        paramsMenu.y = 100;

        backButton.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                Toast.makeText(getApplicationContext(), "Back Button", Toast.LENGTH_SHORT).show();

                return false;

            }
        });

        menuButton.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                Toast.makeText(getApplicationContext(), "Menu Button", Toast.LENGTH_SHORT).show();
                return false;

            }
        });

        // this code is for dragging the chat head
        homeButton.setOnTouchListener(new View.OnTouchListener() {
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Intent startMain = new Intent(Intent.ACTION_MAIN);
                startMain.addCategory(Intent.CATEGORY_HOME);
                startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(startMain);

                return false;
            }
        });
        windowManager.addView(homeButton, paramsHome);
        windowManager.addView(backButton, paramsBack);
        windowManager.addView(menuButton, paramsMenu);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (homeButton != null)
            windowManager.removeView(homeButton);
        if (backButton != null)
            windowManager.removeView(backButton);
        if (menuButton != null)
            windowManager.removeView(menuButton);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}

的AndroidManifest.xml

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>

        <service android:name="com.example.chatheads.ChatHeadService" >
        </service>
    </application>

</manifest>

0 个答案:

没有答案