我尝试制作浮动小部件覆盖按钮 - 就像信使个人资料照片小部件一样,当有人向您发送消息时会显示该小部件。我使用了这些文件。 当我运行程序时,代码中没有任何无效内容。我看到带有按钮的应用程序,但单击后没有任何可见内容。
MainActivity.java:
package com.example.mywidget;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startFloatingWidgetService();
};
private void startFloatingWidgetService() {
Intent intent = new Intent(this, FloatingWidgetService.class);
startService(intent);
}
};
FloatingWidgetService.java
package com.example.mywidget;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
public class FloatingWidgetService extends Service {
private WindowManager mWindowManager;
private View mFloatingWidgetView;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mFloatingWidgetView = LayoutInflater.from(this).inflate(R.layout.floating_widget_layout, null);
// Inicializace tlačítka
Button button = mFloatingWidgetView.findViewById(R.id.floating_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showTextDialog();
}
});
// Inicializace WindowManager
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.BOTTOM | Gravity.END;
mWindowManager.addView(mFloatingWidgetView, params);
}
@Override
public void onDestroy() {
super.onDestroy();
if (mFloatingWidgetView != null && mWindowManager != null) {
mWindowManager.removeView(mFloatingWidgetView);
}
}
private void showTextDialog() {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Floating Widget Service" />
</androidx.constraintlayout.widget.ConstraintLayout>
floating_widget_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/floating_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dfdsfdasdfasdfAFASD"
android:layout_gravity="end"
android:background="@android:color/holo_red_light"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Mywidget"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
FloatingWidgetService.java 中的 showTextDialog() 方法是空的,这就是为什么当你调用这个空方法时什么也没有出现:
private void showTextDialog() {
}
尝试测试它是否正常工作,然后将其替换为您想要的代码:
private void showTextDialog() {
Toast.makeText(this, "Button clicked!", Toast.LENGTH_SHORT).show();
}