I want to make Programm where a countdown is included. I want the countdown to keep on running even if the app is closed. I know i should use getter and setter.
Edit: Every time I press the button, I get an error. Does any one know how i can fix this?
starting the Service:
public void onClick(View arg0) { //countdown starts by preesing a button
// TODO Auto-generated method stub
ssf = Integer.parseInt(essf.getText().toString());
Intent openCountDownService = new Intent("julian.mangelberger.ultravioletobservation.COUNTDOWNSERVICE");
startService(openCountDownService);
}
Service:
import julian.mangelberger.ultravioletobservation.TabMainActivity;
import android.app.IntentService;
import android.content.Intent;
public class CountDownService extends IntentService {
public CountDownService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent intent) {
while (TabMainActivity.progressBarStatus <= 100) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
TabMainActivity.progressBarStatus++;
TabMainActivity.progressbar1.setProgress(TabMainActivity.progressBarStatus);
}
}
}
Related
I am trying to make a demo project, As per this project whenever user click on edittext in any activity, the activity of my application gets started
So I am confuse regarding that whenever we click on edittext in any activity in any app, whether its fb, whatsapp, LINE etc the keyboard automatically appears
does it use broadcastreceiver or InputMethodService for this
and if its use InputMethodService and if i am not wrong this InputMethodService is a type of Service and service has a demerit that the system sometimes kills background services automatically to free memory. But the keyboard appears everytime we click on edittext.
So can anyone tell please how the keyboard everytime appears whenever we click on edittext.
Thanks
InputMethodService is the service which detects when to show the android Keyboard,
This service is an android system service.
You could try making Service and do a research on it. You can use the code below and implement your intent in showSoftInput () method.
import android.inputmethodservice.InputMethodService;
import android.os.IBinder;
import android.os.ResultReceiver;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputBinding;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodSession;
import android.view.inputmethod.InputMethodSubtype;
public class MyService extends InputMethodService implements InputMethod{
#Override
public void attachToken(IBinder token) {
// TODO Auto-generated method stub
}
#Override
public void bindInput(InputBinding binding) {
// TODO Auto-generated method stub
}
#Override
public void changeInputMethodSubtype(InputMethodSubtype subtype) {
// TODO Auto-generated method stub
}
#Override
public void createSession(SessionCallback callback) {
// TODO Auto-generated method stub
}
#Override
public void hideSoftInput(int flags, ResultReceiver resultReceiver) {
// TODO Auto-generated method stub
}
#Override
public void restartInput(InputConnection inputConnection,
EditorInfo attribute) {
// TODO Auto-generated method stub
}
#Override
public void revokeSession(InputMethodSession session) {
// TODO Auto-generated method stub
}
#Override
public void setSessionEnabled(InputMethodSession session, boolean enabled) {
// TODO Auto-generated method stub
}
#Override
public void showSoftInput(int flags, ResultReceiver resultReceiver) {
// TODO Auto-generated method stub
// Run your APP here
//Intent to your acticity
}
#Override
public void startInput(InputConnection inputConnection, EditorInfo info) {
// TODO Auto-generated method stub
}
#Override
public void unbindInput() {
// TODO Auto-generated method stub
}
}
I'm trying to have this central manager class that handles the speech recognition and output voice data. So far, I have failed. This is what the class looks like but my app crashes when I try implementing it in other classes. Can someone please help me out?
Cheers!
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
public class speechEngineR extends Activity {
SpeechRecognizer ears;
TextToSpeech tts;
Intent i;
Context mCon = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ears = SpeechRecognizer.createSpeechRecognizer(mCon);
i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, mCon.getPackageName());
tts = new TextToSpeech(mCon, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
});
ears.setRecognitionListener(new RecognitionListener() {
#Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
#Override
public void onResults(Bundle results) {
// TODO Auto-generated method stub
}
#Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
}
#Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onError(int error) {
// TODO Auto-generated method stub
}
#Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
}
#Override
public void onBufferReceived(byte[] buffer) {
// TODO Auto-generated method stub
}
#Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
}
});
}
public speechEngineR(Context c){
mCon = c;
}
public void outSpeech(String out){
tts.speak(out, TextToSpeech.QUEUE_FLUSH, null);
}
}
And yes, I have I added the following permission in the Manifest.xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Your question and code does not help.
In your code it seems you are trying to use Speech to Text(Audio recorder), and you are calling it using TTS (Text to Speech) which does exactly the opposite of what you are trying to accomplish i.e. speech to text.
Can you make it a bit more clear what you are trying to do??
I have a sound file running in a loop on a service.
The activity that starts the service also has a button. When I press the button I want the sound file in the service to stop playing, i.e kill the service. Have been unsuccessful so far as when I call stopService() the service continues to run.
Any Ideas? Thanks.
Activity
public class AlarmPage extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_page);
TextView timeview = (TextView)findViewById(R.id.timefiled);
final Button dismissButton =(Button)findViewById(R.id.dismissButton);
Bundle b = getIntent().getExtras();
String hrval = b.getString("HR");
String minval = b.getString("MN");
timeview.setText(hrval + ":" + minval);
final Intent alarmring = new Intent(this, alarmringService.class);
startService(alarmring);
dismissButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent alarmmenu = new Intent(getApplicationContext(),SetAlarmsActivity.class);
startActivity(alarmmenu);
System.out.println("Button:" + dismissButton.isPressed());
stopService(alarmring);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.alarm_page, menu);
return true;
}
}
Service
public class alarmringService extends IntentService{
public alarmringService(){
super("alarmringService");
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.drawable.startrekcommsound);
mp.setLooping(true);
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}
}
I can't see why it doesn't work since I'm not an expert, but to fix your problem you could use intents. By the way, IntentService stops itself when it has no more work!
Try this: Send to the IntentService an Intent with a command saying to stop itself.
Then in onHandleIntent() check if this was the request and call stopSelf() from inside the Service.
Moreover, I don't know how MediaPlayer works, but maybe it has his own thread and you should stop this thread too!
In my project I am using binder mechanism to communicate to remote service. The remote service class will call JNI function and updates to UI for every 10 seconds. The JNI function has below code
static int n = 100;
return ++n;
This JNI function calling in Service class and updating to UI like below
public class MyService extends Service{
private static final String TAG = "MyService";
private final Handler serviceHandler = new Handler();
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("inside service onCreate");
Log.d(TAG, "entered onStart");
serviceHandler.removeCallbacks(sendUpdatesToUI);
serviceHandler.postDelayed(sendUpdatesToUI, 1000); // 1 second
}
1. private IMyService.Stub bioStub = new IMyService.Stub() {
2.
3. #Override
4. public int intFromJNI() throws RemoteException {
5. // TODO Auto-generated method stub
6. int libData = Abcd.intFromJNI();
7. System.out.println("inside ****** stub"+libData);
8. return libData;
}
};
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("inside binder ");
return bioStub;
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
Log.d(TAG, "entered Runnable");
try {
bioStub.intFromJNI();
serviceHandler.postDelayed(this, 10000);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
This service class is calling in Activty
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Intent serviceIntent=new Intent();
serviceIntent.setClass(context, MyService.class);
boolean ok=bindService(serviceIntent, mServiceConnection , Context.BIND_AUTO_CREATE);
Log.v("ok", String.valueOf(ok));
}
private ServiceConnection mServiceConnection=new ServiceConnection(){
#Override
public void onServiceConnected(ComponentName arg0, IBinder service) {
// TODO Auto-generated method stub
System.out.println("inside ServiceConnection");
myService = IMyService.Stub.asInterface(service);
try {
Log.d("Client", "entered mServiceConnection");
8. int jniValue = myService.intFromJNI();
9. System.out.println("value if JNI==>"+jniValue);
10.
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
};
Here my problem is, The service class is not updating UI for every 10 seconds and the value is not incremeting in UI but the value is incrementing for every 10 seconds in service class i.e the print statement below is updating for every 10 seconds which is in line Number 7 in above code(Service).
System.out.println("inside ****** stub"+libData);
But I want to it update in UI also. i.e the statement line num 10.
System.out.println("value if JNI==>"+jniValue);
I`t is not happening in my code . How to solve this one .`
You are missing any way for the Service to tell the Activity about new events. The call from the Activity to the Service, intFromJNI, is a one-off function call which happens just once - it has no ongoing responsibility for telling the UI about changes in future.
You should add some listener class. For example, add IMyServiceListener.aidl with something like this:
package com.something;
interface IMyServiceListener {
void valueUpdated(int newValue);
}
And then in IMyService.aidl add this:
import com.something.IMyServiceListener;
void addListener(in IMyServiceListener newListener);
Then, within your IMyService.Stub class you will need to implement addListener - you should add the resulting object to some array of listeners, and call valueUpdated on each listener, each time the value changes.
Within the activity, you should call myService.addListener and pass in some object which receives notification of the changes. You can then display that on the UI within the activity.
Is it alright to register for C2DM in my splash screen?
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class myMain extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.isplash);
MediaPlayer mpSplash = MediaPlayer.create(this, R.raw.musicsplash);
mpSplash.start();
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
registrationIntent.putExtra("sender", "my email address");
startService(registrationIntent);
Thread logoTimer = new Thread(){
public void run(){
try{
sleep(4000);
startActivity(new Intent("com.ishop.pizzaoven.CLEARSCREEN"));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
Yes. You can register for C2DM wherever you like, and often the sooner the better, so the app is ready to receive the messages. Note you do not need to re-register EVERY time the app runs, register once, store it to prefs, and only register again if the pref is empty (e.g. after an uninstall/reinstall)