首页 > 其他分享 >Android之调用service的方法

Android之调用service的方法

时间:2023-04-20 13:46:09浏览次数:38  
标签:BackgroundMusicService 调用 service void VERSION Override new Android public

MainActivity.java :

private BackgroundMusicService caller;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent svc = new Intent(getApplicationContext(), BackgroundMusicService.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        startService(svc);
        bindService(svc, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                BackgroundMusicService.BackgroundMusicServiceBinder binder = (BackgroundMusicService.BackgroundMusicServiceBinder) service;
                caller = binder.getService();
                //caller.customMet();//调用自定义方法
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {
                System.out.println("onServiceDisconnected");
            }
        }, Context.BIND_AUTO_CREATE);
    }
}

BackgroundMusicService.java :

public class BackgroundMusicService extends Service {
    private static final String CHANNEL_ID = "channelID";
    Notification customNotification;
    private final IBinder binder = new BackgroundMusicServiceBinder();

    public class BackgroundMusicServiceBinder extends Binder {
        public BackgroundMusicService getService() {
            return BackgroundMusicService.this;
        }
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("bind");
        return binder;
    }

        @Override
    public void onCreate() {
        super.onCreate();
        setNotification();
        startForeground(200, customNotification);
    }


    private void setNotification() {
        RemoteViews rv = new RemoteViews(getPackageName(), R.layout.custom_notification);
        customNotification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.more)
                .setCustomContentView(rv)
                .build();
        NotificationChannel channel = null;
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channel = new NotificationChannel(CHANNEL_ID, "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
    }

    //自定义方法
    public void customMet(){
        System.out.println("test");
    }
}

AndroidManifest.xml

<Application>
    <service
        android:name=".service.BackgroundMusicService"
        android:exported="true"
        android:enabled="true"
    />
</Application>

标签:BackgroundMusicService,调用,service,void,VERSION,Override,new,Android,public
From: https://www.cnblogs.com/laremehpe/p/17336487.html

相关文章

  • Android UI组件
    1.TextView知识点:autoLink:文本自动识别为web超链接、email地址等——第五章AcitonBar用法:自定义ActionBar——第四章创建Acticity选项菜单:让返回键在ActionBar中显示后,重写onOptionsItemSelected——第四章创建,注册监听器,实现按钮功能——第四章布局:<?xmlversion="1.0......
  • fcitx5-android 安卓开源输入法
    fcitx5-android/fcitx5-android:Anattempttorunfcitx5onAndroid(github.com) Fcitx5forAndroid|F-Droid-FreeandOpenSourceAndroidAppRepository......
  • webservice 客户端生成命令
    wsdl2java-frontendjaxws21-client-pcom.creditease.webapp.webservice.voices-dd:/http://10.105.48.14:8893/Service.svc?wsdlwsdl2java-frontendjaxws21-client-pcom.creditease.webapp.webservice.voices-dd:/http://10.105.48.14:......
  • Ingress nginx配置同一个域名不同的path访问不同的service
    配置同一个域名,不同的path,访问不同的service  #重写URL  #当您访问http://<ingress_ip>/foo/bar时,nginxingresscontroller将把请求路由到foo-service的80端口,并将原始请求的路径/foo/bar重写为/bar。    #nginx.ingress.kubernetes.io/rewrite-ta......
  • 部署kubernetes-dashboard顺便搞懂kubernetes中的ServiceAccount和RBAC
    "种草"kubernetes-dashboard安装部署dashboard创建用于登录面板的ServiceAccount权限控制"种草"kubernetes-dashboardKubernetesDashboard是通用的用于管理Kubernetes集群的WebUI面板kubernetes-dashboard代码库readme中对自己的介绍:KubernetesDashboardi......
  • ShareSDK Android SDK API
    获取用户信息接口(showUser)//cn.sharesdk.framework/***获取用户信息*@paramaccount获取指定账号的用户信息,直接设置成null*/publicvoidshowUser(Stringaccount)示例代码//设置授权登录的平台Platformplat=ShareSDK.getPlatform(QQ.NAME);//授权回调监听,监......
  • 2012 年最佳 Android 应用
    2012年最佳Android应用  •Evernote:云笔记应用•Zappos:美国鞋类电商Zappos官方应用•Pinterest:图片社交网络•Grimm’sSnowWhite:游戏《格林童话:白雪公主》•Expedia:在线旅游网站•Pocket:“稍后再读”应用•Ancestry:家谱网站Ancestry官方应用•Fancy:创意收集应用•......
  • 类中自定义函数并调用and使用钩子函数打印类中变量
    在一个类中自定义一个函数A,并在前向传播函数forword中调用这个函数假设您正在编写一个PyTorch模型,您可以按照以下方式在类中定义函数A,并在forward函数中调用它:importtorchimporttorch.nnasnnclassMyModel(nn.Module):def__init__(self):super(MyMod......
  • 小程序调用图片,视频,相机功能
    //调用图片和相机choosePic(){console.log("table")var_this=thiswx.chooseImage({count:9,//最多可以选择的图片张数,默认9sizeType:['original','compressed'],//original原图,compressed压缩图,默认二者都有sourceType:......
  • 从申请到调用:全国快递物流查询 API 使用教程
    引言面对越来越多的快递需求和快递公司的日益增多,手动查询快递状态的工作变得愈发繁琐。此时,一个全国快递物流查询API的出现能够极大地提高查询的效率和准确性,解决人工查询的问题,为用户提供更加便捷的服务体验。全国快递物流查询API可以通过接口自动查询快递状态并返回相应信......