我在网上搜索了如何制作像Facebook的聊天头一样的弹出窗口。
我找到了以下代码:http://www.piwai.info/chatheads-basics/
public class ChatHeadService extends Service {
private WindowManager windowManager;
private ImageView chatHead;
@Override public IBinder onBind(Intent intent) {
// Not used
return null;
}
@Override public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.ic_launcher);
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(chatHead, params);
}
@Override
public void onDestroy() {
super.onDestroy();
if (chatHead != null) windowManager.removeView(chatHead);
}
}
在我的主要活动中:
startService(new Intent(context, ChatHeadService.class));
我使用了许可:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
但我没有收到弹出通知。甚至不是他的偶像。 你能帮我说一下怎么回事? 感谢。
答案 0 :(得分:0)
检查您是否已将服务添加到AndroidManifest.xml
<service android:name=".ChatHeadService" ></service>
答案 1 :(得分:0)
尝试在项目中复制/粘贴它,或者您可以创建一个新项目并将其复制/粘贴到其中(使用所需的导入)。
MainActivity.class
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
// Method to start the service
public void startService(View view) {
startService(new Intent(getBaseContext(), ChatHeadService.class));
}
// Method to stop the service
public void stopService(View view) {
stopService(new Intent(getBaseContext(), ChatHeadService.class));
}
}
ChatHeadService.class
public class CloseChatHead extends Service {
private WindowManager windowManager;
private ImageView close;
@Override
public IBinder onBind(Intent intent) {
// Not used
return null;
}
WindowManager.LayoutParams mParams;
public WindowManager.LayoutParams getParams(){
return mParams;
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
close = new ImageView(this);
close.setImageResource(R.drawable.circle_close);
final WindowManager.LayoutParams params1 = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params1.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
//params1.x = 0;
//params1.y = 100;
mParams=params1;
close.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:
Toast.makeText(getApplicationContext(),"ACTION_DOWN 2",Toast.LENGTH_SHORT).show();
initialX = params1.x;
initialY = params1.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
return true;
case MotionEvent.ACTION_MOVE:
params1.x = initialX
+ (int) (event.getRawX() - initialTouchX);
params1.y = initialY
+ (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(close, params1);
return true;
}
return false;
}
});
windowManager.addView(close, params1);
}
@Override
public void onDestroy() {
super.onDestroy();
if (close != null)
windowManager.removeView(close);
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.chathead.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnStartService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="startService"
android:text="start_service" />
<Button
android:id="@+id/btnStopService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="stopService"
android:text="stop_service" />
</LinearLayout>
</RelativeLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chathead"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<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=".ChatHeadService" />
<service android:name=".CloseChatHead" />
</application>
</manifest>