以编程方式扩展所有通知或一个包通知

时间:2015-08-30 05:50:42

标签: android

我在网上搜索了如何扩展通知栏,找到了不同API的一些答案,但所有人都使用了行Object service = getSystemService( "statusbar" );,当我输入这一行时,它会在"状态栏&下给出错误#34;表达式,这是我的代码:

 try{
            Object service  = getSystemService("statusbar");
            Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
            Method expand = statusbarManager.getMethod("expand");
            expand.invoke(service);
        }
        catch(Exception ex) {
        }

以下是完整错误:

Must be one of: Context.POWER_SERVICE,
Context.WINDOW_SERVICE, 
Context.LAYOUT_INFLATER_SERVICE, 
Context.ACCOUNT_SERVICE,
Context.ACTIVITY_SERVICE, 
Context.ALARM_SERVICE, 
Context.NOTIFICATION_SERVICE, 
Context.ACCESSIBILITY_SERVICE, 
Context.CAPTIONING_SERVICE, 
Context.KEYGUARD_SERVICE, 
Context.LOCATION_SERVICE, 
Context.SEARCH_SERVICE, 
Context.SENSOR_SERVICE, 
Context.STORAGE_SERVICE, 
Context.WALLPAPER_SERVICE, 
Context.VIBRATOR_SERVICE, 
Context.CONNECTIVITY_SERVICE, 
Context.NETWORK_STATS_SERVICE, 
Context.WIFI_SERVICE, 
Context.WIFI_P2P_SERVICE, 
Context.NSD_SERVICE, 
Context.AUDIO_SERVICE, 
Context.FINGERPRINT_SERVICE, 
Context.MEDIA_ROUTER_SERVICE, 
Context.TELEPHONY_SERVICE, 
Context.TELEPHONY_SUBSCRIPTION_SERVICE, 
Context.CARRIER_CONFIG_SERVICE, 
Context.TELECOM_SERVICE, 
Context.CLIPBOARD_SERVICE, 
Context.INPUT_METHOD_SERVICE, 
Context.TEXT_SERVICES_MANAGER_SERVICE, 
Context.APPWIDGET_SERVICE, 
Context.DROPBOX_SERVICE, 
Context.DEVICE_POLICY_SERVICE, 
Context.UI_MODE_SERVICE, 
Context.DOWNLOAD_SERVICE,
Context.NFC_SERVICE, 
Context.BLUETOOTH_SERVICE, 
Context.USB_SERVICE,
Context.LAUNCHER_APPS_SERVICE, 
Context.INPUT_SERVICE, 
Context.DISPLAY_SERVICE,
Context.USER_SERVICE, 
Context.RESTRICTIONS_SERVICE, 
Context.APP_OPS_SERVICE,
Context.CAMERA_SERVICE, 
Context.PRINT_SERVICE, 
Context.CONSUMER_IR_SERVICE, 
Context.TV_INPUT_SERVICE, 
Context.USAGE_STATS_SERVICE, 
Context.MEDIA_SESSION_SERVICE, 
Context.BATTERY_SERVICE, 
Context.JOB_SCHEDULER_SERVICE, 
Context.MEDIA_PROJECTION_SERVICE, 
Context.MIDI_SERVICE less... (Ctrl+F1) 

显示更多内容后出现此错误:

此检查查看Android API调用,并确保将正确类型的资源传递给期望给定类型资源的int参数;它检查期望RGB颜色整数的API是否传递实际颜色而不是颜色资源;它检查需要特定权限的API是否具有在清单中声明的​​权限;它检查预期落在给定范围内的参数是否实际;它会检查调用者是否查看某些方法调用的结果,依此类推。

我的完整代码:

public class NotificationService extends NotificationListenerService {
Context context;

@Override
public void onCreate() {
    super.onCreate();
    context = getApplicationContext();
}

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    try{
        Object service = getSystemService("statusbar");
        Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
        Method expand = statusbarManager.getMethod("expand");
        expand.invoke(service);
    }
    catch(Exception ex) {
    }
    String pack = sbn.getPackageName();
    String ticker = sbn.getNotification().tickerText.toString();
    Bundle extras = sbn.getNotification().extras;
    String title = extras.getString("android.title");
    String text = extras.getCharSequence("android.text").toString();
    Log.i("Package", pack);
    Log.i("Ticker", ticker);
    Log.i("Title", title);
    Log.i("Text", text);
    if ("com.whatsapp".equals(pack)) {
        Intent newIntent = new Intent("Msg");
        newIntent.putExtra("package", pack);
        newIntent.putExtra("ticker", ticker);
        newIntent.putExtra("title", title);
        newIntent.putExtra("text", text);
        LocalBroadcastManager.getInstance(context).sendBroadcast(newIntent);
    }
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
    Log.i("Msg", "Notification Removed");
}

}

代码是在后台运行的服务,当收到通知后,服务会获取详细信息,我通常会收到来自XXX&#39;的消息。而不是消息,所以我想扩展它们,这就是发生了什么:PI也试图把它放在另一个类上,活动本身在onRecieve广播中,因为我读到某个地方你不能在活动之外使用getSystemService但仍然是一样的。

1 个答案:

答案 0 :(得分:0)

您是否在清单中添加了权限?

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
相关问题