下面列出了怎么用android.app.INotificationManager的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onPackagesChanged(boolean removingPackage, String[] pkgList, int[] uid) {
if (removingPackage) {
INotificationManager inm = NotificationManager.getService();
if (pkgList != null && (pkgList.length > 0)) {
for (String pkgName : pkgList) {
try {
inm.removeAutomaticZenRules(pkgName);
inm.setNotificationPolicyAccessGranted(pkgName, false);
} catch (Exception e) {
Slog.e(TAG, "Failed to clean up rules for " + pkgName, e);
}
}
}
}
super.onPackagesChanged(removingPackage, pkgList, uid);
}
/**
* Show the view for the specified duration.
*/
public void show() {
if (mNextView == null) {
throw new RuntimeException("setView must have been called");
}
INotificationManager service = getService();
String pkg = mContext.getOpPackageName();
TN tn = mTN;
tn.mNextView = mNextView;
try {
service.enqueueToast(pkg, tn, mDuration);
} catch (RemoteException e) {
// Empty
}
}
public MediaSessionService(Context context) {
super(context);
mSessionManagerImpl = new SessionManagerImpl();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mMediaEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleMediaEvent");
mLongPressTimeout = ViewConfiguration.getLongPressTimeout();
mNotificationManager = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
mPackageManager = AppGlobals.getPackageManager();
}
/** @hide */
protected final INotificationManager getNotificationInterface() {
if (mNoMan == null) {
mNoMan = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
}
return mNoMan;
}
/**
* Request that the listener be rebound, after a previous call to {@link #requestUnbind}.
*
* <p>This method will fail for listeners that have
* not been granted the permission by the user.
*/
public static void requestRebind(ComponentName componentName) {
INotificationManager noMan = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
try {
noMan.requestBindListener(componentName);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Request that the service be unbound.
*
* <p>Once this is called, you will no longer receive updates and no method calls are
* guaranteed to be successful, until you next receive the {@link #onListenerConnected()} event.
* The service will likely be killed by the system after this call.
*
* <p>The service should wait for the {@link #onListenerConnected()} event
* before performing this operation. I know it's tempting, but you must wait.
*/
public final void requestUnbind() {
if (mWrapper != null) {
INotificationManager noMan = getNotificationInterface();
try {
noMan.requestUnbindListener(mWrapper);
// Disable future messages.
isConnected = false;
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
}
private final INotificationManager getNotificationInterface() {
if (mNoMan == null) {
mNoMan = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
}
return mNoMan;
}
/**
* Request that the provider be rebound, after a previous call to (@link #requestUnbind).
*
* <p>This method will fail for providers that have not been granted the permission by the user.
*/
public static final void requestRebind(ComponentName componentName) {
INotificationManager noMan = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
try {
noMan.requestBindProvider(componentName);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Request that the provider service be unbound.
*
* <p>This will no longer receive subscription updates and will not be able to update the
* state of conditions until {@link #requestRebind(ComponentName)} is called.
* The service will likely be killed by the system after this call.
*
* <p>The service should wait for the {@link #onConnected()} event before performing this
* operation.
*/
public final void requestUnbind() {
INotificationManager noMan = getNotificationInterface();
try {
noMan.requestUnbindProvider(mProvider);
// Disable future messages.
mProvider = null;
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
static private INotificationManager getService() {
if (sService != null) {
return sService;
}
sService = INotificationManager.Stub.asInterface(ServiceManager.getService("notification"));
return sService;
}
static private INotificationManager getService() {
if (sService != null) {
return sService;
}
sService = INotificationManager.Stub.asInterface(ServiceManager
.getService("notification"));
return sService;
}
@Override
public void onBootPhase(int phase) {
if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
LocalServices.getService(ActivityManagerInternal.class)
.registerScreenObserver(this);
mNotificationManager = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
synchronized (mLock) {
Looper looper = Looper.getMainLooper();
Handler handler = new Handler(looper);
ArrayList<EnabledComponentChangeListener> listeners = new ArrayList<>();
listeners.add(this);
mComponentObserver = EnabledComponentsObserver.build(mContext, handler,
Settings.Secure.ENABLED_VR_LISTENERS, looper,
android.Manifest.permission.BIND_VR_LISTENER_SERVICE,
VrListenerService.SERVICE_INTERFACE, mLock, listeners);
mComponentObserver.rebuildAll();
}
//TODO: something more robust than picking the first one
ArraySet<ComponentName> defaultVrComponents =
SystemConfig.getInstance().getDefaultVrComponents();
if (defaultVrComponents.size() > 0) {
mDefaultVrService = defaultVrComponents.valueAt(0);
} else {
Slog.i(TAG, "No default vr listener service found.");
}
DisplayManager dm =
(DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE);
mVr2dDisplay = new Vr2dDisplay(
dm,
LocalServices.getService(ActivityManagerInternal.class),
LocalServices.getService(WindowManagerInternal.class),
mVrManager);
mVr2dDisplay.init(getContext(), mBootsToVr);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_USER_UNLOCKED);
getContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_USER_UNLOCKED.equals(intent.getAction())) {
VrManagerService.this.setUserUnlocked();
}
}
}, intentFilter);
}
}
/**
* Directly register this service with the Notification Manager.
*
* <p>Only system services may use this call. It will fail for non-system callers.
* Apps should ask the user to add their listener in Settings.
*
* @param context Context required for accessing resources. Since this service isn't
* launched as a real Service when using this method, a context has to be passed in.
* @param componentName the component that will consume the notification information
* @param currentUser the user to use as the stream filter
* @hide
* @removed
*/
@SystemApi
public void registerAsSystemService(Context context, ComponentName componentName,
int currentUser) throws RemoteException {
if (mWrapper == null) {
mWrapper = new NotificationListenerWrapper();
}
mSystemContext = context;
INotificationManager noMan = getNotificationInterface();
mHandler = new MyHandler(context.getMainLooper());
mCurrentUser = currentUser;
noMan.registerListener(mWrapper, componentName, currentUser);
}
/**
* Directly unregister this service from the Notification Manager.
*
* <p>This method will fail for listeners that were not registered
* with (@link registerAsService).
* @hide
* @removed
*/
@SystemApi
public void unregisterAsSystemService() throws RemoteException {
if (mWrapper != null) {
INotificationManager noMan = getNotificationInterface();
noMan.unregisterListener(mWrapper, mCurrentUser);
}
}