I am following this tutorial to implement facebook like chat head. When I start the service from an activity it just work fine. But when I start it from a broadcast receiver the chat head is not showing.And when I open the app it shows.Can I show the chat head from a broadcast receiver, without using an activity? Actaully Iam using this feature like a callerid window. So that it will show up when I receive a call. Here is ChatHead service,
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);
}
}
And in onReceive method of my broadcast receiver I am showing the chat head using,
Intent callerid = new Intent(
context,
CallerIDService.class);
callerid.putExtra("name", "basim");
callerid.putExtra("number", "123456");
context.startService(callerid);