如何创建用于开始和停止录音的小部件

发布于 2024-11-17 02:25:08 字数 2783 浏览 1 评论 0原文

在我的项目中,我创建了一个小部件类、一个服务和一个远程服务,但是我无法 调用启动或停止它的方法。我在这里有示例代码

用于小部件

            Intent intent = new Intent(context, med_service.class);

            PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);
            Toast.makeText(context, pi.toString() + intent.toString(), Toast.LENGTH_SHORT).show();

            views.setOnClickPendingIntent(R.id.button1, pi);
            Toast.makeText(context, pi.toString() + intent.toString(), Toast.LENGTH_SHORT).show();

用于远程服务

public class service extends Service implements IBinder{

public IBinder onBind(Intent intent) {
    Log.i("RemoteService", "onBind() called");
    return new RemoteServiceImpl();
  }
  /**
   * The IRemoteInterface is defined through IDL
   */


  public class RemoteServiceImpl extends IRemoteService.Stub {

    @Override
    public void start() throws RemoteException {
        // TODO Auto-generated method stub

        Toast.makeText(getApplicationContext(), "inside start method", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void stop() throws RemoteException {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "inside stop method", Toast.LENGTH_SHORT).show();

    }
  }

用于服务

public class med_service extends Service{

IRemoteService mService;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    Log.i("RemoteService", "onBind() called");
    Toast.makeText(getApplicationContext(), "service not bind to connection", Toast.LENGTH_SHORT).show();

    return new service();


}

class RemoteServiceConnection implements ServiceConnection {

    public void onServiceConnected(ComponentName className, IBinder service ) {
                mService = IRemoteService.Stub.asInterface(service);
            }
            public void onServiceDisconnected(ComponentName className) {
                mService = null;
            }
    };

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{

    try{

            RemoteServiceConnection mConnection = new RemoteServiceConnection();
            getApplicationContext().bindService(new Intent(IRemoteService.class.getName()), mConnection, Context.BIND_AUTO_CREATE);

        }
    catch(Exception e)
        {
            Toast.makeText(getApplicationContext(), "service not bind to connection", Toast.LENGTH_SHORT).show();
        }

        try {
            mService.start();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 1;
}

感谢任何类型的帮助

提前谢谢

In my project I have created a widget class, a service and a remote service, however I'm not able to
call the methods for starting or stopping it. I've sample codes here

For widget

            Intent intent = new Intent(context, med_service.class);

            PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);
            Toast.makeText(context, pi.toString() + intent.toString(), Toast.LENGTH_SHORT).show();

            views.setOnClickPendingIntent(R.id.button1, pi);
            Toast.makeText(context, pi.toString() + intent.toString(), Toast.LENGTH_SHORT).show();

for remote service

public class service extends Service implements IBinder{

public IBinder onBind(Intent intent) {
    Log.i("RemoteService", "onBind() called");
    return new RemoteServiceImpl();
  }
  /**
   * The IRemoteInterface is defined through IDL
   */


  public class RemoteServiceImpl extends IRemoteService.Stub {

    @Override
    public void start() throws RemoteException {
        // TODO Auto-generated method stub

        Toast.makeText(getApplicationContext(), "inside start method", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void stop() throws RemoteException {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "inside stop method", Toast.LENGTH_SHORT).show();

    }
  }

for service

public class med_service extends Service{

IRemoteService mService;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    Log.i("RemoteService", "onBind() called");
    Toast.makeText(getApplicationContext(), "service not bind to connection", Toast.LENGTH_SHORT).show();

    return new service();


}

class RemoteServiceConnection implements ServiceConnection {

    public void onServiceConnected(ComponentName className, IBinder service ) {
                mService = IRemoteService.Stub.asInterface(service);
            }
            public void onServiceDisconnected(ComponentName className) {
                mService = null;
            }
    };

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{

    try{

            RemoteServiceConnection mConnection = new RemoteServiceConnection();
            getApplicationContext().bindService(new Intent(IRemoteService.class.getName()), mConnection, Context.BIND_AUTO_CREATE);

        }
    catch(Exception e)
        {
            Toast.makeText(getApplicationContext(), "service not bind to connection", Toast.LENGTH_SHORT).show();
        }

        try {
            mService.start();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 1;
}

any kind of help is appreciated

Thank You in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房敞 2024-11-24 02:25:08

使用 getService 而不是 getActivity

use getService instead of getActivity

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文