在android中无法以编程方式启用和禁用服务

时间:2018-05-22 07:07:52

标签: android service

我正在使用fcm服务进行推送通知。在我的应用程序中,我可以选择为我所做的事情选择通知我使用组件以编程方式启用和禁用服务,以便如果用户选择通知我将启用它否则禁用但它不起作用。

代码:

       public  void disableComponent(){
            PackageManager pm = getPackageManager();
            pm.setComponentEnabledSetting(new ComponentName(this, MyFirebaseMesagingService.class),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        }
        public  void enableComponent() {
            PackageManager pm = getPackageManager();
            pm.setComponentEnabledSetting(new ComponentName(this, MyFirebaseMesagingService.class),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
        }
     <service
                android:name=".MyFirebaseMesagingService">
                <intent-filter>
                    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
                </intent-filter>
            </service>
i tried with this still not working

 public  void disableComponent(){
        PackageManager pm = getPackageManager();
        pm.setComponentEnabledSetting(new ComponentName("com.example.sgorle.fcmsampleapp", "com.example.sgorle.fcmsampleapp.MyFirebaseMesagingService"),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    }
    public  void enableComponent() {
        PackageManager pm = getPackageManager();
        pm.setComponentEnabledSetting(new ComponentName("com.example.sgorle.fcmsampleapp", "com.example.sgorle.fcmsampleapp.MyFirebaseMesagingService"),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    }

0 个答案:

没有答案