Git Product home page Git Product logo

Comments (6)

adustdu2015 avatar adustdu2015 commented on July 21, 2024 1

Android10以后,启动前台服务,必须显示通知的处理方案

我们都知道,目前启动服务在Android O 之后,必须使用startForeground 。并且会显示一个通知栏的东西。

//调用
Intent intent = new Intent(MainActivity.this, MyService.class);
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  	startForegroundService(intent);
  } else {
  	startService(intent);
  }

我们完全可以用bindService(intent,conn,flags)替代。

bindService用于绑定一个服务。这样当bindService(intent,conn,flags)后,就会绑定一个服务。这样做可以获得这个服务对象本身;

废话不多说,直接上代码:

 /**
 *1:调用者
 */
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
 
/**
 * 此例的目的就是拿到MyService的引用,从而可以引用其内部的方法和变量
 * 
 * @author Administrator
 * 
 */
public class MainActivity extends AppCompatActivity {
	//这里创建一个静态对象,就可以在其他类中进行调用啦。
	public static MyService myService;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
 
		Intent intent = new Intent(this, MyService.class);
		bindService(intent, connection, Context.BIND_AUTO_CREATE);
	}
 
	private ServiceConnection connection = new ServiceConnection() {
 
		@Override
		public void onServiceDisconnected(ComponentName name) {
			myService = null;
		}
 
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			myService = ((MyService.MyBinder) service).getService();
			System.out.println("Service连接成功");
			// 执行Service内部自己的方法
			myService.excute();
		}
	};
 
	protected void onDestroy() {
		super.onDestroy();
		unbindService(connection);
	};
}
 
 /*
 *2:服务者
 */
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
 
public class MyService extends Service {
	private final IBinder binder = new MyBinder();
 
	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}
 
	public class MyBinder extends Binder {
		public MyService getService() {
			return MyService.this;
		}
	}
 
	public void excute() {
		System.out.println("通过Binder得到Service的引用来调用Service内部的方法");
	}
 
	@Override
	public void onDestroy() {
		// 当调用者退出(即使没有调用unbindService)或者主动停止服务时会调用
		super.onDestroy();
	}
 
	@Override
	public boolean onUnbind(Intent intent) {
		// 当调用者退出(即使没有调用unbindService)或者主动停止服务时会调用
		System.out.println("调用者退出了");
		return super.onUnbind(intent);
	}
}

3.AndroidManifest.xml添加服务
 <application ...>
 	<service android:name=".MyService" />
 </application>

完美解决了。

from cactus.

nukix avatar nukix commented on July 21, 2024

一样, 2021年6月1日更新系统的 Android 11 都出现这种情况。

from cactus.

BaiPeiHe avatar BaiPeiHe commented on July 21, 2024

这样就可以了,不会报错了 Cactus.getInstance().hideNotification(false)

from cactus.

nukix avatar nukix commented on July 21, 2024

@BaiPeiHe 这样会显示常驻通知栏的, 很多用户对于常驻通知栏很反感

from cactus.

github-gsjb avatar github-gsjb commented on July 21, 2024

@BaiPeiHe 这样会显示常驻通知栏的, 很多用户对于常驻通知栏很反感

该怎么解决?

from cactus.

cjecky avatar cjecky commented on July 21, 2024

一起讨论下

from cactus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.