无法启动Android服务

时间:2015-03-05 00:08:42

标签: android android-intent android-activity android-service

我正在尝试使构建一个简单的服务有点Facebook聊天,但由于某种原因我无法启动该服务,每当我点击启动服务按钮没有任何反应。如果有人可以帮助我,告诉我我做错了什么,我真的很感激。感谢

FloatService.java

package com.example.justeen.floatexample;

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;


public class FloatService extends Service{

private WindowManager windowManager;
private ImageView floatIcon;

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

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

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    floatIcon = new ImageView(this);
    floatIcon.setImageResource(R.drawable.android_head);

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

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

    windowManager.addView(floatIcon, params);

    try {
        floatIcon.setOnTouchListener(new View.OnTouchListener() {
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        return true;
                    case MotionEvent.ACTION_UP:
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);
                        windowManager.updateViewLayout(floatIcon, params);
                        return true;
                }
                return false;
            }
        });
    } catch (Exception e) {
        // TODO: handle exception
    }
}
@Override
public void onDestroy() {
    super.onDestroy();
    if (floatIcon != null) windowManager.removeView(floatIcon);
}

}

Activity.Java

package com.example.justeen.floatexample;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;


public class HelloWorldActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hello_world);
    Bundle bundle = getIntent().getExtras();
    if(bundle != null && bundle.getString("LAUNCH").equals("YES")) {
        startService(new Intent(HelloWorldActivity.this, FloatService.class));

        Button launch = (Button) findViewById(R.id.button1);
        launch.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startService(new Intent(HelloWorldActivity.this, FloatService.class));
            }
        });

        Button stop = (Button) findViewById(R.id.button2);
        stop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                stopService(new Intent(HelloWorldActivity.this, FloatService.class));
            }
        });
    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_hello_world, menu);
    return true;
}

}

Manifet.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.justeen.floatexample" >
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".HelloWorldActivity"
        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.justeen.floatexample.FloatService"
        android:enabled="true">
    </service>
</application>

</manifest>

1 个答案:

答案 0 :(得分:0)

你必须删除捆绑检查,因为它是一种没有意义的复制粘贴。在主活动中首先调用onCreate时,Bundle值将为null,因此不会将click侦听器附加到您的服务。删除捆绑检查,一切都会有效。