这是我的代码
FloatingCircle 是由 MainActivity.class
启动的服务当我单击通知中的关闭按钮时,我想调用方法 Visibility()
public class FloatingCircle extends Service {
public void onCreate () {
super.onCreate();
initializeView();
getScreenSize();
showFloat();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "channel-id";
String channelName = "Channel Name";
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= 26) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
Intent intent = new Intent(getApplicationContext(), FloatingCircle.class **// What should be here could you suggest me**);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.mipmap.ic_launcher)//R.mipmap.ic_launcher
.setContentTitle("Bubble Life")
.setContentText("Touch to On/Off")
.addAction(R.drawable.ic_action_name,
"Dismiss", pIntent)// Here Dismiss is Button shown in notification
.setDefaults(NotificationCompat.DEFAULT_ALL);
notificationManager.notify(0, mBuilder.build());
}
}
This method must be invoked when Dismiss Button is clicked
public void Visibility(()
{
//some code here
windowManager.removeViewImmediate(smallCircle);
Log.d("lol","Done Killing");
}
public void killbubble()
{
Visibility();
selfstop();
}
我尝试了创建另一项服务并调用Floatingclass方法的一件事。...
public class MethodCaller extends Service {
FloatingCircle floatingCircle=new FloatingCircle();
@Override
public void onCreate() {
super.onCreate();
Log.d("lol","yeah");
//Toast.makeText(getApplicationContext(),"lol",Toast.LENGTH_LONG).show();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
floatingCircle.killbubble();
stopSelf();
return flags;
}
}
我已经成功了,但这给了我一个错误...
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void android.view.WindowManager.removeViewImmediate(android.view.View)' on a null object reference
at com.anam.floatimage.model.FloatingCircle.Visibility(FloatingCircle.java:547)
at com.anam.floatimage.model.FloatingCircle.killbubble(FloatingCircle.java:777)
at com.anam.floatimage.MethodCaller.onStartCommand(MethodCaller.java:36)