mardi 4 août 2015

How to keep running the task even if the app is already closed?

Currently, i have built a service to do httpRequest to my php server every 15 seconds. The code works well but the problem is when i close the app the httpRequest is stop working and does not send data anymore. what i want to do is when i close the app, the httpRequest still running. what should i change in my code?

here is the code i have been working on...

public class NotificationService extends Service {

public String response;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "on start command", Toast.LENGTH_SHORT).show();
    new MyAsyncTask().execute("muhammad sappe");
    return START_NOT_STICKY;
}

private class MyAsyncTask extends AsyncTask<String, String, String>{

  @Override
  protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    postData(params[0]);
    return null;
  }

  @Override
  protected void onPostExecute(String result) {
      new Handler().postDelayed(new Runnable() {
            public void run() {
                new MyAsyncTask().execute("muhammad sappe");
            }
        }, 15000);
    super.onPostExecute(result);
  }

}

public void postData(String MyName){
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://mydomain/getData.php");
    try {   
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("action", MyName));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        // Execute HTTP Post Request
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        response = httpclient.execute(httppost, responseHandler);
    }catch(ClientProtocolException e){
        // TODO Auto-generated catch block
    }catch(IOException e) {
        // TODO Auto-generated catch block
    }
}

public void onDestroy() {
    super.onDestroy();
}
}

ListView first Item overlaid

My ListView displays in first Item something else as the rest of the ListView

The Problem is, that it replaces the first item instead of taking a own position.

Example how it has to be:

A (diferent to the others)

B

B

B

How it is:

A (B Overlaid by A)

B

B

As you can see, one B is missing because it is overlaid by A.

Here is the Code. I wrote my own CustomAdapter:

@Override
public int getCount() {
    if (taskItems.size() + 1 >= 0)
        return taskItems.size();
    return 0;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

public static class ViewHolder {
    public TextView taskTitleTextView;
    public TextView taskObjectTextView;
    public TextView taskLocationTextView;
    public ImageView taskImageImageView;

}

public static class ViewHolderProjects {
    public TextView projectTitelTextView;
    public TextView projectInfoTextView;
    public TextView projectDeadline;
    public ImageView projectImageImageView;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    ViewHolderProjects holderProjects;
    if (position == 0) {
        if (convertView == null) {
            vi = inflater.inflate(R.layout.list_item_projects, null);
            holderProjects = new ViewHolderProjects();

            holderProjects.projectTitelTextView = (TextView) vi.findViewById(R.id.projectTitle);
            holderProjects.projectInfoTextView = (TextView) vi.findViewById(R.id.projectInfo);
            holderProjects.projectImageImageView = (ImageView) vi.findViewById(R.id.projectImage);
            holderProjects.projectDeadline = (TextView) vi.findViewById(R.id.projectdeadline);

            vi.setTag(holderProjects);
        } else {
            holderProjects = (ViewHolderProjects) vi.getTag();
        }
        if (projectMap.size() <= 0) {
            holderProjects.projectInfoTextView.setText("Keine Infos");
            holderProjects.projectTitelTextView.setText("Kein Titel");
        } else {
            if (projectMap.get(position).containsKey("project_company_image")) {
                if (projectMap.get(position).get("project_company_image").getBytes().length == 0) {
                    holderProjects.projectTitelTextView.setText(jobMap.get(position).get("job_subject"));
                    holderProjects.projectDeadline.setText(jobMap.get(position).get("job_deadline"));
                    return vi;
                }
                Bitmap bm = Bitmap.createBitmap(base64EncodeDecode.decodeBase64(projectMap.get(position).get("project_company_image")));
                holderProjects.projectTitelTextView.setText(jobMap.get(position).get("job_subject"));
                holderProjects.projectDeadline.setText(jobMap.get(position).get("job_deadline"));
                holderProjects.projectImageImageView.setImageBitmap(bm);
            }
        }
    } else {

        ViewHolder holder;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.list_item_tasks, null);

            holder = new ViewHolder();
            holder.taskTitleTextView = (TextView) vi.findViewById(R.id.taskTitle);
            holder.taskObjectTextView = (TextView) vi.findViewById(R.id.taskObject);
            holder.taskLocationTextView = (TextView) vi.findViewById(R.id.taskLocation);
            holder.taskImageImageView = (ImageView) vi.findViewById(R.id.taskImage);

            vi.setTag(holder);
        } else {
            holder = (ViewHolder) vi.getTag();
        }

        if (taskItems.size() <= 0) {
            holder.taskTitleTextView.setText("Kein Titel");
            holder.taskObjectTextView.setText("Kein Objekt");
            holder.taskLocationTextView.setText("Kein Standort");
        } else {
            if (taskItems.get(position).containsKey("object_image")) {
                if (taskItems.get(position).get("object_image").getBytes().length == 0) {
                    holder.taskTitleTextView.setText(taskItems.get(position).get("task_headline"));
                    holder.taskObjectTextView.setText(taskItems.get(position).get("object_name"));
                    holder.taskLocationTextView.setText(taskItems.get(position).get("object_location"));

                    return vi;
                }
            }
            Bitmap bm = Bitmap.createBitmap(base64EncodeDecode.decodeBase64(taskItems.get(position).get("object_image")));
            holder.taskTitleTextView.setText(taskItems.get(position).get("task_headline"));
            holder.taskObjectTextView.setText(taskItems.get(position).get("object_name"));
            holder.taskLocationTextView.setText(taskItems.get(position).get("object_location"));
            holder.taskImageImageView.setImageBitmap(bm);
        }
    }
    return vi;

}

how to change colour of action bar tabs on click for a few seconds only in android

I am using action bar tabs(not tabhost) . I want to change colour of my selected tab for a second (like whtsapp). I have used this code but its not working and this code is also hiding my tabs text.

public void onTabSelected(Tab tab, FragmentTransaction ft)
{
   TextView v=new TextView(TabActivity.this);
   Log.d("TextView", v.toString());
   v.setBackgroundColor(Color.GREEN);  
   tab.setCustomView(v);
}

Optimizing Memory in Android when managing Fragment Transactions

I'm using Fragments in my app where all of them share one base activity, so activity is not destroyed till app closes. When I navigate from one fragment to another, I have used the following code:

 Fragment myFragment=new MyFragment();
 FragmentManager fragmentManager = getFragmentManager();
 fragmentManager.beginTransaction().addToBackStack("MyFragment")
 .replace(R.id.content_frame, myFragment,"AirtimeFragment").commit();

When using it I have to create a new Object for each fragment which is not good for memory consumption.

So I want to create one global method which manages all fragment in which it checks whether the fragment already exists, then no need to recreate it just uses the existing fragment object, so I can reduce memory consumption?

TextInputLayout error after enter value into edittext

How can i hide a TextInputLayout error after input one text in EditText. Is it possible?

how can i achieve this or I am doing something wrong here.!!

enter image description here

code

    layoutEdtPhone =(TextInputLayout)rootView.findViewById(R.id.layoutEdtPhone);
    layoutEdtPhone.setErrorEnabled(true);
    layoutEdtPhone.setError(getString(R.string.ui_no_phone_toast));
    layoutEdtPassword =   (TextInputLayout)rootView.findViewById(R.id.layoutEdtPassword);
    layoutEdtPassword.setErrorEnabled(true);
    layoutEdtPassword.setError(getString(R.string.ui_no_password_toast));

    edtPhone=(EditText)rootView.findViewById(R.id.edtPhone);
    edtPassword=(EditText)rootView.findViewById(R.id.edtPassword);

xml

            <EditText
                android:id="@+id/edtPhone"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="100dp"
                android:background="@drawable/edt_background_selector"
                android:drawableLeft="@drawable/phone_icon"
                android:drawableStart="@drawable/phone_icon"
                android:hint="@string/phone"
                android:inputType="phone"
                android:padding="5dip"
                android:singleLine="true"
                android:textSize="14sp" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layoutEdtPassword"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/edtPassword"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:background="@drawable/edt_background_selector"
                android:drawableLeft="@drawable/password_icon"
                android:drawableStart="@drawable/password_icon"
                android:hint="@string/password"
                android:inputType="textPassword"
                android:padding="5dip"
                android:singleLine="true"
                android:textSize="14sp" />
        </android.support.design.widget.TextInputLayout>

When i run my Fragment project, eclipse shows errors. I don't know how to fix it

Here is the instruction i tried to follow carefully:

http://ift.tt/1k5dyDx

here are the first errors appear in Logcat:

E/MoreInfoHPW_ViewGroup(21404): Parent view is not a TextView

E/AndroidRuntime(21404): FATAL EXCEPTION: main

E/AndroidRuntime(21404): Process: com.example.fragmentstest, PID: 21404

E/AndroidRuntime(21404): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentstest/com.example.fragmentstest.MainActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class fragment

here are my MainActivity and activity_main codes:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void selectFrag(View view){
    Fragment fr;
    if (view == findViewById(R.id.button2)){
        fr = new FragmentTwo();
    } else {
        fr = new FragmentOne();
    }
    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_place, fr);
    fragmentTransaction.commit();


}  




<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Fragment No.1"
    android:onClick="selectFrag" />

<Button
    android:id="@+id/button2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Fragment No.2" 
    android:onClick="selectFrag"/>

<fragment 
    android:name="com.example.fragmentstest.FragmentOne"
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>"

</LinearLayout>

Show heads-up notification in cordova android

Anyone knows how to do heads-up notifications in cordova android?

I'm making an app where it is critical that a user sees a notification. I'm using the phonegap-build pushPlugin.

The server is a nodejs where i have tried:

var androidPayload = {
    data: {
        title: title,
        message: msg,
        notId:title,
        priority: 2
    }
};

or have i made something wrong?