How to create widget for start and stop voice recording
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");
r开发者_如何学Ceturn 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
use getService instead of getActivity
精彩评论