I have an app that uses the callIntent = new Intent(Intent.ACTION_CALL) to make a call. I need a way to get notified when the user ends the call.
I used to use telephonyManager.listen but its been deprecated. I see I am supposed to use registerTelephonyCallback but I cant find out how that works.
Can anyone give me some pointers how to do this
Thanks
I solved it as follows
public class MyCallStateListener extends TelephonyCallback implements TelephonyCallback.CallStateListener {
private String TAG = "MyCallStateListener";
#Override
public void onCallStateChanged(int state) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: {
Log.v(TAG, "&&&&&&&&&&&&&&&&&&&&&&&&&& state is CALL_STATE_IDLE");
break;
}
case TelephonyManager.CALL_STATE_OFFHOOK: {
Log.v(TAG, "&&&&&&&&&&&&&&&&&&&&&&&&&&& state is CALL_STATE_OFFHOOK");
break;
}
default: {
Log.v(TAG, "state is " + state);
}
}
}
}
TelephonyManager telephonyManager =
(TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE);
TelephonyCallback telephonyCallback = new MyCallStateListener();
telephonyManager.registerTelephonyCallback(getContext().getMainExecutor(), telephonyCallback);
Related
I don't know why i have tow errors one at ClickListener() and ShowListener()? I am trying to like OnItemClickListener but for MeowBottomNavigation
getSupportActionBar().hide();
bottomNavigation =findViewById(R.id.bottomNavigation);
bottomNavigation.add(new MeowBottomNavigation.Model(1, R.drawable.ic_baseline_message));
bottomNavigation.add(new MeowBottomNavigation.Model(2, R.drawable.ic_settings));
bottomNavigation.add(newMeowBottomNavigation.Model(3,R.drawable.ic_baseline_account_circle_24));
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new FragmentChatt()).commit();
bottomNavigation.setOnClickMenuListener(new MeowBottomNavigation.**ClickListener()** {
public void onClickItem(MeowBottomNavigation.Model item) {
//Toast.makeText(getApplicationContext(),"Clicked item"+item.getId(),Toast.LENGTH_SHORT).show();
}
});
bottomNavigation.setOnShowListener(new MeowBottomNavigation.**ShowListener()** {
public void onShowItem(MeowBottomNavigation.Model item) {
Fragment select_fragment=null;
switch (item.getId()){
case ID_CHAT:
select_fragment=new FragmentChatt();
break;
case ID_SETTINGS:
select_fragment=new FragmentSettengs();
break;
case ID_ACOUNT:
select_fragment=new FragmentAcount();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,select_fragment).commit();
}
});
}
}
I know this is an old post now, but for anyone who's still having the same problem check your module implementation in build.gradle file.
Here's the a dependency that worked for me:
dependencies {
implementation 'com.etebarian:meow-bottom-navigation-java:1.2.0'
}
You can also check their github:
Documentation
But that is a deprecated solution, so here's the new one they recommend New Documentation.
If you want to use the new solution, you need to implement the dependency:
dependencies {
implementation 'com.etebarian:meow-bottom-navigation:1.3.1'
}
And change the setOnShowListener and setOnClickMenuListener methods with:
bottomNavigation.setOnClickMenuListener(new Function1<MeowBottomNavigation.Model, Unit>() {
#Override
public Unit invoke(MeowBottomNavigation.Model model) {
// YOUR CODES
return null;
}
});
bottomNavigation.setOnShowListener(new Function1<MeowBottomNavigation.Model, Unit>() {
#Override
public Unit invoke(MeowBottomNavigation.Model model) {
// YOUR CODES
return null;
}
});
I am trying to unzip a folder using Android's AsyncTask. The class (called Decompress) is an inner class of Unzip where Unzip itself is a non-Activity class. The pseudo-code is:
public class Unzip {
private String index;
private String unzipDest; //destination file for storing folder.
private Activity activity;
private boolean result; //result of decompress.
public void unzip(String loc) {
Decompress workThread = new Decompress(loc, activity);
workThread.execute();
if(unzip operation was successful) {
display(index);
}
//Class Decompress:
class Decompress extends AsyncTask<Void, Integer, Boolean> {
private ProgressDialog pd = null;
private Context mContext;
private String loc;
private int nEntries;
private int entriesUnzipped;
public Decompress(String location, Context c) {
loc = location;
mContext = c;
nEntries = 0;
entriesUnzipped = 0;
Log.v(this.toString(), "Exiting decompress constructor.");
}
#Override
protected void onPreExecute() {
Log.v(this.toString(), "Inside onPreExecute.");
pd = new ProgressDialog(mContext);
pd.setTitle("Unzipping folder.");
pd.setMessage("Unzip in progress.");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
Log.v(this.toString(), "Showing dialog and exiting.");
pd.show();
}
#Override
protected Boolean doInBackground(Void... params) {
//unzip operation goes here.
unzipDest = something; //unzip destination is set here.
if(unzip operation is successful) {
result = true;
index = url pointing to location of unzipped folder.
} else {
result = false;
}
}
#Override
protected void onPostExecute(Boolean result) {
if(result) {
if(pd != null) {
pd.setTitle("Success");
pd.setMessage("folder is now ready for use.");
pd.show();
pd.dismiss();
pd = null;
Log.v(this.toString(), "Unzipped.");
index = unzipDest + "/someURL";
Log.v(this.toString(), "index present in: " + index);
}
} else {
pd = ProgressDialog.show(mContext, "Failure", "Cannot unzip.");
pd.dismiss();
}
}
}
Problems I am facing:
1. The value of unzipDest and index, updated in doInBackground, remain null to Unzip and all its objects. How can I ensure that the values remain updated?
2. I know that doInBackground occurs in a thread separate from the main UI thread. Does that mean that any values updated in the new thread will be lost once that thread returns?
How can I ensure that the values remain updated?
They will be updated since they are member variables. However, since AsyncTask is asynchrounous, they might not be updated yet when you check them. You can use an interface to create a callback when these values are updated. This SO answer covers how to do this
Does that mean that any values updated in the new thread will be lost once that thread returns?
No they shouldn't be "lost". They probably just haven't been changed in the AsyncTask when you check them.
Since this isn't your actual code I can't see when you are trying to access them but you can use the interface method or call the functions that need these values in onPostExecute(). You also can do a null check before trying to access them. It just depends on the functionality and flow that you need as to which is the best way. Hope that helps.
Edit
In the answer I linked to, you tell the Activity that you will use that interface and override its method(s) with implements AsyncResponse in your Activity declaration after creating the separate interface class
public class MainActivity implements AsyncResponse{
then, in your Activity still, you override the method you declared in that class (void processFinish(String output);)
#Override
void processFinish(String output){ // using same params as onPostExecute()
//this you will received result fired from async class of onPostExecute(result) method.
}
then this is called in onPostExecute() when the listener sees that it is done with delegate.processFinish(result); delegate is an instance of AsyncResponse (your interface class)
public class AasyncTask extends AsyncTask{
public AsyncResponse delegate=null;
#Override
protected void onPostExecute(String result) {
delegate.processFinish(result);
}
Interface example taken from linked answer above and adjusted/commented for clarity. So be sure to upvote that answer if it helps anyone.
I'm getting two errors stating Syntax error on tokens, ConstructorHeaderName expected instead & Syntax error on token "(", < expected
on the line:
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
...any suggestions?
public class DataCountService extends Service {
String text = "USR;1";
String ERROR = Constants.PREFS_NAME;
private Timer timer = new Timer();
private long period;
private long delay_interval;
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
private class EndCallListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {
// Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
//wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
// Log.i(LOG_TAG, "OFFHOOK");
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
//when this state occurs, and your flag is set, restart your app
// Log.i(LOG_TAG, "IDLE");
}
}
}
The line
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
is not included in any method. You should put this in the constructor(which seems like what you want) or a regular method.
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
Should be inside a method, not directly inside class. Refer this question for example implementation.
I have an xml file in my RCP application. I am displaying it to user using FormEditor.
public class MyFormEditor extends FormEditor implements IResourceChangeListener{
public MyFormEditor(){
ResourcePlugin.getWorkspace.addResourceChangeListener(this);
...
}
#Override
public void resourceChanged(IResourceChangeEvent event){
int type = event.getType();
switch(type){
IResourceChangeEvent.PRE_DELETE:
IResourceChangeEvent.PRE_CLOSE:
this.close(true);
break;
IResourceChangeEvent.POST_CHANGE:
System.out.println("Resource is change.");
break;
default:
break;
}
}
#Override
public void dispose(){
ResourcePlugin.getWorkspace.removeResourceChangeListener(this);
super.dispose();
}
}
IResourceChange.POST_CHANGE event gets triggered when I save resource or I update the resource from SVN repository.
Under IResourceChange.POST_CHANGE how to determine resource is updated from SVN?
I tried following thing but it didn't work for me.
IResourceDelta delta = event.getDelta;
int flags = delta.getFlags();
boolean sync = (flags & IResourceDelta.SYNC) != 0;
if(sync){
System.out.println("Resource updated from server.");
}
Do let me know if you need any other info.
Like lots of askers on SO, I'm relatively new to java and have attempted to teach myself android programming with some decent success. I'm sure this is trivial to someone with actual knowledge on the subject. I'm working on a app that attempts to fetch data from the net and 'returns true' if you get the data and 'returns false' if it doesn't. I want to do something when it returns false but can't figure out how to properly handle the response. Right now, I just ignore the response an do nothing. Any help?
public void onBackPressed() {
Someclass.getinfo().maybeShowInfo(this);
finish();
}
What I would like to do is something like (in pseudo code)
public void onBackPressed() {
Someclass.getinfo().maybeShowInfo(this);
// if false is returned
// do something
// else
// finish();
}
public void onBackPressed() {
boolean result = Someclass.getinfo().maybeShowInfo(this);
if (result) {
finish();
} else {
// do something else
}
}
It looks to me like you've combined two things that must be separate. Make fetching the data and displaying two methods, by two classes.
private InfoDao infoDao; // This is a class that gets the data; it's a member of the class with the onBackPressed() method
public void onBackPressed() {
Info info = this.infoDao.find();
if (info != null) {
displayInfo();
}
}
public void onBackPressed()
{
boolean result = Someclass.getinfo().maybeShowInfo(this);
if (result = false)
{
//do work for false response;
}
else
{
finish();
}
}
don't forget that you have to make your Someclass.getinfo() return true if it succeded and false if it didn't.