When i click on digit button on app and then on cos for example, works fine.
When i click on cos and number, return -1.
For example on display TextView i see:
1cos
0.540302320
if i put:
cos1
-1
I use a switch case.
private double operateAritmetic(String a, String b, String op){
switch (op){
case "+": return (Double.valueOf(a) + Double.valueOf(b));
case "-": return (Double.valueOf(a) - Double.valueOf(b));
case "*": return (Double.valueOf(a) * Double.valueOf(b));
case "/": try{
return (Double.valueOf(a) / Double.valueOf(b));
}catch (Exception e){Log.d("Calc",e.getMessage());
}
default: return -1;
}
}
private double operateTrigonometric(String a, String op){
switch (op){
case "Sin":
return(Math.sin(Double.valueOf(a)));
case "Cos":
return(Math.cos(Double.valueOf(a)));
case "Tan":
try{
return(Math.tan(Double.valueOf(a)));
}catch(Exception e){
Log.d("Calc", e.getMessage());
}
case "sqrt":
return(Math.sqrt(Double.valueOf(a)));
default: return -1;
}
}
public void onClickEqual(View v){
String[] operation=display.split(Pattern.quote(currentOperator));
Double result;
if(operation.length==1) {
result = operateTrigonometric(operation[0],currentOperator);
_screen.setText(display + "\n" + String.valueOf(result));
}
else if (operation.length<2)
return;
else {
result = operateAritmetic(operation[0], operation[1], currentOperator);
_screen.setText(display + "\n" + String.valueOf(result));
}
}
When a video is being downloaded there is a cancel option to cancel the download.but when i cancel the download i don't get any callback from DownloadManager to update the app UI.
This cancle option is only available in Android 7 .
Or else is there any option to hide or remove the cancel option from there.
After registering your broadcast receiver for DownloadManager.ACTION_DOWNLOAD_COMPLETE, you can check for DownloadManager status in the onReceive() method of the DownloadManager broadcast as follows -
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
checkDownloadStatus();
}
};
private void checkDownloadStatus(){
// TODO Auto-generated method stub
DownloadManager.Query query = new DownloadManager.Query();
long id = preferenceManager.getLong(strPref_Download_ID, 0);
query.setFilterById(id);
Cursor cursor = downloadManager.query(query);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
switch(status){
case DownloadManager.STATUS_FAILED:
String failedReason = "";
switch(reason){
case DownloadManager.ERROR_CANNOT_RESUME:
failedReason = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
failedReason = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
failedReason = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
failedReason = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
failedReason = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
failedReason = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
failedReason = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
failedReason = "ERROR_UNHANDLED_HTTP_CODE";
break;
case DownloadManager.ERROR_UNKNOWN:
failedReason = "ERROR_UNKNOWN";
break;
}
Toast.makeText(AndroidDownloadManagerActivity.this,
"FAILED: " + failedReason,
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_PAUSED:
String pausedReason = "";
switch(reason){
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
pausedReason = "PAUSED_QUEUED_FOR_WIFI";
break;
case DownloadManager.PAUSED_UNKNOWN:
pausedReason = "PAUSED_UNKNOWN";
break;
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
pausedReason = "PAUSED_WAITING_FOR_NETWORK";
break;
case DownloadManager.PAUSED_WAITING_TO_RETRY:
pausedReason = "PAUSED_WAITING_TO_RETRY";
break;
}
Toast.makeText(AndroidDownloadManagerActivity.this,
"PAUSED: " + pausedReason,
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_PENDING:
Toast.makeText(AndroidDownloadManagerActivity.this,
"PENDING",
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_RUNNING:
Toast.makeText(AndroidDownloadManagerActivity.this,
"RUNNING",
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_SUCCESSFUL:
Toast.makeText(AndroidDownloadManagerActivity.this,
"SUCCESSFUL",
Toast.LENGTH_LONG).show();
downloadManager.remove(id);
break;
}
}
}
I have a listView that I want to display text files in the row folder corresponding to the list item that has been clicked. My code so far:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String displaytxt = "";
int fileLen = 0;
try {
InputStream text = null;
switch(position) {
case 1:
text = getResources().openRawResource(R.raw.books1);
break;
case 2:
text = getResources().openRawResource(R.raw.books2);
break;
case 3:
text = getResources().openRawResource(R.raw.books3);
break;
default:
break;
}
fileLen = text.available();
byte[] fileBuffer = new byte[fileLen];
text.read(fileBuffer);
text.close();
displaytxt = new String (fileBuffer);
}
catch (IOException e) {
}
tv.setText(displaytxt);
}
But when I click the first item on the list I get an error in the emulator
Try to change:
switch(position) {
case 0:
text = getResources().openRawResource(R.raw.books1);
break;
case 1:
text = getResources().openRawResource(R.raw.books2);
break;
case 2:
text = getResources().openRawResource(R.raw.books3);
break;
default:
text = getResources().openRawResource(R.raw.books1); // paste default book
break;
}
Is there a way to detect when the system starts a download and get information of it and force to download it to specific location? I googled it and results where not much helpful
Start this service class and then start your download it will print the result for status of download.
public class MyService extends Service {
private Timer timer;
private TimerTask timerTask;
private static final int SAMPLING_RATE = 1000;
public MyService() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
#Deprecated
public void onStart(Intent intent, int startId) {
Log.e("Download", "onstart");
}
#Override
public void onCreate() {
timer = new Timer();
timerTask = new TimerTask() {
#Override
public void run() {
// Log.d(MyService.class.toString(),
// "tic ... "+System.currentTimeMillis());
getDownloadData();
}
};
if (timer != null && timerTask != null) {
timer.schedule(timerTask, 0, SAMPLING_RATE);
}
}
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
#SuppressLint("NewApi")
public void getDownloadData() {
DownloadManager downloadMgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.STATUS_FAILED
| DownloadManager.STATUS_PENDING
| DownloadManager.STATUS_RUNNING
| DownloadManager.STATUS_SUCCESSFUL);
Cursor c = downloadMgr.query(query);
if (c == null) {
System.out.println("--------------------------");
} else {
if (c.moveToFirst()) {
System.out.println("------------End--------------");
while (c.isAfterLast() == false) {
getStatus(c);
c.moveToNext();
}
}
}
}
public void getStatus(Cursor c) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
int columnReason = c.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = c.getInt(columnReason);
int status = c.getInt(columnIndex);
String statusText = null;
String reasonText = null;
switch (status) {
case DownloadManager.STATUS_FAILED:
statusText = "STATUS_FAILED";
switch (reason) {
case DownloadManager.ERROR_CANNOT_RESUME:
reasonText = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
reasonText = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
reasonText = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
reasonText = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
reasonText = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
reasonText = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
reasonText = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
reasonText = "ERROR_UNHANDLED_HTTP_CODE";
break;
case DownloadManager.ERROR_UNKNOWN:
reasonText = "ERROR_UNKNOWN";
break;
}
break;
case DownloadManager.STATUS_PAUSED:
statusText = "STATUS_PAUSED";
switch (reason) {
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
reasonText = "PAUSED_QUEUED_FOR_WIFI";
break;
case DownloadManager.PAUSED_UNKNOWN:
reasonText = "PAUSED_UNKNOWN";
break;
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
reasonText = "PAUSED_WAITING_FOR_NETWORK";
break;
case DownloadManager.PAUSED_WAITING_TO_RETRY:
reasonText = "PAUSED_WAITING_TO_RETRY";
break;
}
break;
case DownloadManager.STATUS_PENDING:
statusText = "STATUS_PENDING";
break;
case DownloadManager.STATUS_RUNNING:
statusText = "STATUS_RUNNING";
break;
case DownloadManager.STATUS_SUCCESSFUL:
statusText = "STATUS_SUCCESSFUL";
break;
}
Log.d("status", statusText + " " + reasonText);
}
}
I have Activity with 20 buttons and GridView.
One Button clear GridView and other add to GridView different values.
Some time I push 1-Add, 2-Clear, 3-Add but see that button clear becomes pushed(Orange color) and executed 1-Add, 2-Add, 3-Clear.
Some time button clear becomes pushed long time, and don't answer at all, but other buttons work.
How is it possible?
My activity:
public class Smena extends Activity
{
public Nomenklatura Asortiment;
GridView gvMain;
GridView gvMain1;
GridView gvMain2;
ArrayAdapter<String> adapter;
ArrayAdapter<String> adapter1;
ArrayAdapter<String> adapter2;
double Sum=0;
String Sdacha;
String filename="shops345";
Boolean PushX=false;
TextView tv1;
void Save()
{
LocalPersistence.witeObjectToFile(this, Asortiment, filename);
}
public void myClickHandler(View target){
Intent intent1;
switch (target.getId()){
case R.id.button1:
ButtonOn (Asortiment.SmenaButName[0],Asortiment.SmenaButPrice[0]);
break;
case R.id.button10:
ButtonOn (Asortiment.SmenaButName[9],Asortiment.SmenaButPrice[9]);
break;
case R.id.button11:
ButtonOn (Asortiment.SmenaButName[10],Asortiment.SmenaButPrice[10]);
break;
case R.id.button12:
ButtonOn (Asortiment.SmenaButName[11],Asortiment.SmenaButPrice[11]);
break;
case R.id.button13:
ButtonOn (Asortiment.SmenaButName[12],Asortiment.SmenaButPrice[12]);
break;
case R.id.button14:
ButtonOn (Asortiment.SmenaButName[13],Asortiment.SmenaButPrice[13]);
break;
case R.id.button15:
ButtonOn (Asortiment.SmenaButName[14],Asortiment.SmenaButPrice[14]);
break;
case R.id.button16:
intent1=new Intent(Smena.this,Napitki.class);
Save();
startActivity(intent1);
break;
case R.id.button17:
intent1=new Intent(Smena.this,Zhevacki.class);
Save();
startActivity(intent1);
break;
case R.id.button18:
intent1=new Intent(Smena.this,Dobavki.class);
Save();
startActivity(intent1);
break;
case R.id.button19:
intent1=new Intent(Smena.this,Presmena.class);
Save();
startActivity(intent1);
break;
case R.id.button2:
ButtonOn (Asortiment.SmenaButName[1],Asortiment.SmenaButPrice[1]);
break;
case R.id.button20:
intent1=new Intent(Smena.this,Orp.class);
Asortiment.data.clear();
Asortiment.data1.clear();
Save();
startActivity(intent1);
break;
case R.id.button21:
ButtonDel();
break;
case R.id.button22:
clear();
break;
case R.id.button23:
intent1=new Intent(Smena.this,Sdacha.class);
Save();
startActivity(intent1);
break;
case R.id.button25:
clear();
// checkTv();
break;
case R.id.button3:
ButtonOn (Asortiment.SmenaButName[2],Asortiment.SmenaButPrice[2]);
break;
case R.id.button4:
ButtonOn (Asortiment.SmenaButName[3],Asortiment.SmenaButPrice[3]);
break;
case R.id.button41:
if(PushX)
buttonx(3);
else buttonx(2);PushX=true;
break;
case R.id.button42:
if(PushX)
buttonx(4);
else buttonx(3);PushX=true;
break;
case R.id.button43:
if(PushX)
buttonx(5);
else buttonx(4);PushX=true;
break;
case R.id.button44:
if(PushX)
buttonx(6);
else buttonx(5);PushX=true;
break;
case R.id.button45:
if(PushX)
buttonx(7);
else buttonx(6);PushX=true;
break;
case R.id.button46:
if(PushX)
buttonx(8);
else buttonx(7);PushX=true;
break;
case R.id.button5:
ButtonOn (Asortiment.SmenaButName[4],Asortiment.SmenaButPrice[4]);
break;
case R.id.button6:
ButtonOn (Asortiment.SmenaButName[5],Asortiment.SmenaButPrice[5]);
break;
case R.id.button7:
ButtonOn (Asortiment.SmenaButName[6],Asortiment.SmenaButPrice[6]);
break;
case R.id.button8:
ButtonOn (Asortiment.SmenaButName[7],Asortiment.SmenaButPrice[7]);
break;
case R.id.button9:
ButtonOn (Asortiment.SmenaButName[8],Asortiment.SmenaButPrice[8]);
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.smena);
Intent i=getIntent();
Asortiment=(Nomenklatura) LocalPersistence.readObjectFromFile(this, filename);
adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, Asortiment.data);
adapter1 = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, Asortiment.data1);
tv1=(TextView) findViewById(R.id.textView1);
gvMain = (GridView) findViewById(R.id.gridView1);
gvMain.setAdapter(adapter);
gvMain1 = (GridView) findViewById(R.id.gridView11);
gvMain1.setAdapter(adapter1);
gvMain2 = (GridView) findViewById(R.id.gridView2);
if(Asortiment.data2.size()<=0) data2clear();
if(i.getExtras().containsKey("Dobavki"))
{
ArrayList<String> DobavkiList=new ArrayList<String>();
DobavkiList=i.getExtras().getStringArrayList("Dobavki") ;
for(String item : DobavkiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
if(i.getExtras().containsKey("Napitki"))
{
ArrayList<String> NapitkiList=new ArrayList<String>();
NapitkiList=i.getExtras().getStringArrayList("Napitki") ;
for(String item : NapitkiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
if(i.getExtras().containsKey("Zhevacki"))
{
ArrayList<String> ZhevackiList=new ArrayList<String>();
ZhevackiList=i.getExtras().getStringArrayList("Zhevacki") ;
for(String item : ZhevackiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
Double sd;
if(i.getExtras().containsKey("Sdacha"))
{
sd=Double.parseDouble(Asortiment.data2.get(1));
Sdacha=i.getExtras().getString("Sdacha");
sd=Double.parseDouble(Sdacha)-sd;
}
else
{
if(Asortiment.data2.size()>0)
{
Sdacha=Asortiment.data2.get(3);
sd=Double.parseDouble(Asortiment.data2.get(1));
if(Double.parseDouble(Asortiment.data2.get(3))>0)
sd=Double.parseDouble(Asortiment.data2.get(3))-sd;
else sd=(double) 0;
}else {sd=(double) 0;Sdacha="0";}
}
Asortiment.data2.set(3, Sdacha);
Asortiment.data2.set(5, sd.toString());
adapter2 = new ArrayAdapter<String>(this, R.layout.item2, R.id.tvText, Asortiment.data2);
gvMain2.setAdapter(adapter2);
}
void buttonx(int k){
if(!Asortiment.LastButton.get(0).equals("0")){
for (int i=0;i<k;i++){
ButtonOn (Asortiment.LastButton.get(0),Asortiment.LastButton.get(1));
}}
}
void checkTv()
{
Date dt1=new Date();
if(dt1.getHours()>19 && Asortiment.LastRequest.getDay()<dt1.getDay() && (Calendar.DAY_OF_WEEK==2 ||Calendar.DAY_OF_WEEK==4 || Calendar.DAY_OF_WEEK==6))
tv1.setText("ОТПРАВЬТЕ ЗАЯВКУ");
else tv1.setText("");
}
void clear()
{
Asortiment.data.clear();
adapter.clear();
Asortiment.data1.clear();
adapter1.clear();
data2clear();
}
void data2clear()
{
Asortiment.data2.set(1, "0");
Asortiment.data2.set(3, "0");
Asortiment.data2.set(5, "0");
gvMain2.setAdapter(adapter2);
}
private void ButtonDel()
{
int cnt= Asortiment.data.size();
if(cnt>0){
Double d=Double.parseDouble(Asortiment.data2.get(1));//1
d=d-Double.parseDouble(Asortiment.data1.get(cnt-1));
Asortiment.data2.set(1, d.toString());
//adapter2 = new ArrayAdapter<String>(this, R.layout.item2, R.id.tvText, Asortiment.data2);//1
gvMain2.setAdapter(adapter2);
Asortiment.data.remove(cnt-1);
Asortiment.data1.remove(cnt-1);
//adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, Asortiment.data);
//adapter1 = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, Asortiment.data1);
gvMain.setAdapter(adapter);
gvMain1.setAdapter(adapter1);
}
}
private void ButtonOn (String name, String price)
{
PushX=false;
if(name.length()>15)name=name.substring(0, 15);
Asortiment.LastButton.set(0, name);
Asortiment.LastButton.set(1, price);
Asortiment.data.add(name);
Asortiment.data1.add(price);
gvMain.setAdapter(adapter);
gvMain1.setAdapter(adapter1);
Double d=Double.parseDouble(Asortiment.data2.get(1));
d=Double.parseDouble(price)+d;
Asortiment.data2.set(1, d.toString());
Double sd=Double.parseDouble(Asortiment.data2.get(3));
if (sd>0) {
sd=sd-d;
Asortiment.data2.set(5, sd.toString());
}
gvMain2.setAdapter(adapter2);
}
}
To solve the issue, or even to narrow down to the issue, you need to refactor the code. On each click there are a bunch of things that are happening, you might be over flooding the clicks. Are you giving enough time between clicks?
And also, do consider making methods generic, for example
if(i.getExtras().containsKey("Napitki"))
{
ArrayList<String> NapitkiList=new ArrayList<String>();
NapitkiList=i.getExtras().getStringArrayList("Napitki") ;
for(String item : NapitkiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
if(i.getExtras().containsKey("Zhevacki"))
{
ArrayList<String> ZhevackiList=new ArrayList<String>();
ZhevackiList=i.getExtras().getStringArrayList("Zhevacki") ;
for(String item : ZhevackiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
blocks like these can be made as generic methods and may be you would like to follow java conventions.
Just a suggestion :)