I'm trying to update a foreground service where a layout appear in front of other apps like Facebook Messenger's talking head. It has no background service. The foreground service operates successfully without the setView(). When I set the view to the window manager I get
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
I cannot figure out how to set the token to not be null. Any insight would be helpful.
Activity
public class MainActivity extends AppCompatActivity {
Button btnStartService, btnStopService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartService = findViewById(R.id.buttonStartService);
btnStopService = findViewById(R.id.buttonStopService);
btnStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService();
}
});
btnStopService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService();
}
});
}
public void startService() {
Intent serviceIntent = new Intent(this, ForegroundService.class);
serviceIntent.putExtra("inputExtra", "Foreground Service Example in Android");
ContextCompat.startForegroundService(this, serviceIntent);
}
public void stopService() {
Intent serviceIntent = new Intent(this, ForegroundService.class);
stopService(serviceIntent);
}
}
Service
public class ForegroundService extends Service {
int LAYOUT_FLAG;
WindowManager windowManager;
View mFloatingView;
public static final String CHANNEL_ID = "ForegroundServiceChannel";
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String input = intent.getStringExtra("inputExtra");
createNotificationChannel();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Foreground Service")
.setContentText(input)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.build();
Toast.makeText(getApplicationContext(),"onStartCommand",Toast.LENGTH_SHORT).show();
**makeWindow();**
startForeground(1, notification);
//do heavy work on a background thread
//stopSelf();
return START_NOT_STICKY;
}
**public void makeWindow(){**
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.O){
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
}else{
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
windowManager = (WindowManager)this.getSystemService(WINDOW_SERVICE);
mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_widget,null);
WindowManager.LayoutParams layoutParams = new WindowManager
.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
//initial position
layoutParams.gravity = Gravity.TOP | Gravity.END;
layoutParams.x = 0;
layoutParams.y = 100;
mFloatingView.setLayoutParams(layoutParams);
mFloatingView.setVisibility(View.VISIBLE);
**windowManager.addView(mFloatingView,layoutParams);**
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">
<RelativeLayout
android:id="@+id/layout_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="30dp">
<TextView
android:id="@+id/text_widget"
android:layout_width="120dp"
android:layout_height="120dp"
android:textSize="22sp"
android:text="10"
android:textStyle="bold"
android:gravity="center"
android:textColor="@color/white"
android:background="@drawable/back_circle"/>
</RelativeLayout>
</RelativeLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.foregroundservicetester">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ForegroundServiceTester">
<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>
<service
android:name=".ForegroundService" android:enabled="true" android:exported="false" />
</application>
</manifest>