1

我正在按照教程来实现 Facebook 之类的聊天头。当我从一项活动启动服务时,它工作正常。但是当我从广播接收器启动它时,聊天头没有显示。当我打开应用程序时,它会显示。我可以在不使用活动的情况下从广播接收器显示聊天头吗?实际上,我正在使用此功能,例如 callerid 窗口。这样当我接到电话时它就会显示出来。这是ChatHead服务,

public class CallerIDService extends Service {

private WindowManager windowManager;
private ImageView chatHead;
private RelativeLayout parentlayout;
private RelativeLayout border;
Button close;
Button add;
int imageid = 1;
int borderid = 2;
int nameid = 3;
int closeid = 4;
TextView name;
TextView number;
String numbertext;
String nametext;

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Bundle b = intent.getExtras();

    numbertext = b.getString("number");
    nametext = b.getString("name");

    name.setText(nametext);
    number.setText(numbertext);

    Log.i("Incoming in service ", "Incoming in service " + numbertext
            + "--" + nametext);

    return super.onStartCommand(intent, flags, startId);
}

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

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    chatHead = new ImageView(this);
    close = new Button(this);
    add = new Button(this);
    add.setText("Save");
    close.setText("Dismiss");
    close.setId(closeid);

    chatHead.setImageResource(R.drawable.ic_usericon);

    chatHead.setId(imageid);

    parentlayout = new RelativeLayout(this);
    border = new RelativeLayout(this);
    border.setId(borderid);

    parentlayout.setBackgroundColor(Color.parseColor("#ffffff"));
    border.setBackgroundColor(Color.parseColor("#ff0c0c"));

    name = new TextView(this);

    name.setTextColor(Color.parseColor("#494949"));
    name.setId(nameid);
    name.setTextSize(19);

    number = new TextView(this);

    number.setTextColor(Color.parseColor("#696969"));

    number.setTextSize(16);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.FILL_PARENT, 150,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);

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

    final RelativeLayout.LayoutParams params_imageview = new RelativeLayout.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT);

    params_imageview.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

    final RelativeLayout.LayoutParams params_border = new RelativeLayout.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, 3);

    params_border.addRule(RelativeLayout.ALIGN_PARENT_TOP);

    final RelativeLayout.LayoutParams params_name = new RelativeLayout.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT);

    params_name.addRule(RelativeLayout.RIGHT_OF, imageid);
    params_name.addRule(RelativeLayout.CENTER_VERTICAL);

    final RelativeLayout.LayoutParams params_number = new RelativeLayout.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT);

    params_number.addRule(RelativeLayout.RIGHT_OF, imageid);
    params_number.addRule(RelativeLayout.BELOW, nameid);

    final RelativeLayout.LayoutParams params_add = new RelativeLayout.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT);

    params_add.addRule(RelativeLayout.LEFT_OF, closeid);
    params_add.addRule(RelativeLayout.CENTER_VERTICAL);

    final RelativeLayout.LayoutParams params_close = new RelativeLayout.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT);

    params_close.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    params_close.addRule(RelativeLayout.CENTER_VERTICAL);

    // android:layout_alignParentLeft="true"
    // android:layout_below="@+id/relativeLayout1"

    parentlayout.addView(chatHead, params_imageview);// adding user image to
                                                        // view
    parentlayout.addView(border, params_border);// adding top border to view
    parentlayout.addView(name, params_name);
    parentlayout.addView(number, params_number);
    parentlayout.addView(close, params_close);
    parentlayout.addView(add, params_add);

    windowManager.addView(parentlayout, params);

    close.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            stopSelf();

        }
    });

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            addcontact(numbertext, nametext);
            add.setVisibility(View.GONE);

        }
    });



}



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

}

在我的广播接收器的 onReceive 方法中,我正在使用聊天头显示,

Intent callerid = new Intent(
            context,
            CallerIDService.class);
    callerid.putExtra("name", "basim");
    callerid.putExtra("number", "123456");
    context.startService(callerid);
4

2 回答 2

2

您是否添加了以下权限?

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
于 2013-10-19T12:45:34.993 回答
2

我认为这是因为您的服务没有启动,您需要在清单中注册您的服务并让它在没有您的活动的情况下启动。

这是关于该主题的线程。

Android - 在启动时启动服务

于 2013-09-26T16:28:11.160 回答