Tuesday, June 30, 2015

Kill the applications running in background in android

Use this in Activity Class to  killBackgroundProcesses:
   try {
            ActivityManager actvityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
            actvityManager.killBackgroundProcesses(pkgn.toString());// pkgn is a process id /////
        } catch (Exception e) {
            e.printStackTrace();
        }
You also have to add this in manifest
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

Thursday, June 25, 2015

Send SMS from Your Android Application

Use bellow code in Activity class in your own method
public void SendSMSRequest(View view) {
    String SendSMSNumber = "880xxxxxxxxxx";   
    String smsText = "Need Permission";
    String phoneNo = GetSMSNumber.toString();   
    String msg = smsText.toString();  
   try {
        SmsManager smsManager = SmsManager.getDefault();    
        smsManager.sendTextMessage(phoneNo, null, msg, null, null);     
        Toast.makeText(getApplicationContext(), "Message Sent",           
        Toast.LENGTH_LONG).show();    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(),           
        ex.getMessage().toString(),               
        Toast.LENGTH_LONG).show();      
       ex.printStackTrace();    }
}


Use bellow code in XML 
   <Button android:layout_width="225dp"  
           android:layout_height="75dp"  
           android:text="Off Line Play"  
           android:id="@+id/button"    
           android:onClick="SendSMSRequest"
           android:textColor="#ffffff"  
           android:textStyle="bold"   
           android:textSize="21sp"  
           android:layout_above="@+id/subscribe"  
           android:layout_centerHorizontal="true"    />
Use permission in Androidmanifest
<!-- Permission:Sms Related access -->
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />

HTTP POST request to a URL in Android

Use bellow code in your Activity class in your own method
 ///////////////////// Check SubScription ////////////////////
                             try {
                              AsyncHttpClient client = new AsyncHttpClient();
                                // Http Request Params Object
                                RequestParams params = new RequestParams();
                                String u = "B2mGaME";
                                String au = "gamewrapperB2M";
                                // String mob = "880xxxxxxxxxx";
                                params.put("usr", u.toString());
                                params.put("aut", au.toString());
                                params.put("uph", MobileNo.toString());
                                //  params.put("uph", mob.toString());
                                client.post("http://196.6.13.12:80/ws/game_wrapper_reg_check.php", params, new AsyncHttpResponseHandler() {
                                    @Override
                                    public void onSuccess(String response) {
                                        playStatus = response;
                                        gonextpage(playStatus);
                                        Log.i(getClass().getSimpleName(), "Response SP Status. " + playStatus);
                                    }
                @Override
                public void onFailure(Throwable throwable) {
                    super.onFailure(throwable);
                }
                                });
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
You Also need to Add bellow Jar file in libs folde
android-async-http-1.3.1.jar
Finally edit your build.gradle
dependencies {
    compile files('libs/<android-async-http-1.3.1.jar>')

}
In the last Rebuild your project.

How to execute a method by clicking a notification

Use this code to execute a method by click on notification
protected void displayNotification() {
    Log.i("Start", "notification");

  /* Invoking the default notification service */
    NotificationCompat.Builder  mBuilder =
            new NotificationCompat.Builder(this);
    mBuilder.setAutoCancel(true);

    mBuilder.setContentTitle("New Message");
    mBuilder.setContentText("You've received UnRead message.");
    mBuilder.setTicker("New Message Alert!");
    mBuilder.setSmallIcon(R.drawable.icon2);

  /* Increase notification number every time a new notification arrives */
    mBuilder.setNumber(++numMessages);

  /* Creates an explicit intent for an Activity in your app */

    Intent resultIntent = new Intent(this, FreesmsLog.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(FreesmsLog.class);

  /* Adds the Intent that starts the Activity to the top of the stack */
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  /* notificationID allows you to update the notification later on. */
    mNotificationManager.notify(notificationID, mBuilder.build());

}

Vibrate and Sound defaults on notification

Its work fine to me,You can try it.
 protected void displayNotification() {

        Log.i("Start", "notification");

      // Invoking the default notification service //
        NotificationCompat.Builder  mBuilder =
                new NotificationCompat.Builder(this);
        mBuilder.setAutoCancel(true);

        mBuilder.setContentTitle("New Message");
        mBuilder.setContentText("You have "+unMber_unRead_sms +" new message.");
        mBuilder.setTicker("New message from PayMe..");
        mBuilder.setSmallIcon(R.drawable.icon2);

      // Increase notification number every time a new notification arrives //
        mBuilder.setNumber(unMber_unRead_sms);

      // Creates an explicit intent for an Activity in your app //

        Intent resultIntent = new Intent(this, FreesmsLog.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(FreesmsLog.class);

      // Adds the Intent that starts the Activity to the top of the stack //
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);

      //  mBuilder.setOngoing(true);
        Notification note = mBuilder.build();
        note.defaults |= Notification.DEFAULT_VIBRATE;
        note.defaults |= Notification.DEFAULT_SOUND;

        mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      // notificationID allows you to update the notification later on. //
        mNotificationManager.notify(notificationID, mBuilder.build());

    }

ImageButton Not clickable - Android

Use This in Xml
<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:layout_marginBottom="48dp"
        android:onClick="AddInfo"
        android:background="@mipmap/ea_logo"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
Use This in Activity Class  
public void AddInfo(View view) {
///// Use Your Own code /////////

}

Android: Time Loop in milliseconds

You can use timer or thread handler

  timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

               Do your own code
                ///////schedule the timer, after the first 100ms the TimerTask will run every 1000ms
            }
        }, 1000, 1000);
Can Use Another handler
Handler handler = new Handler();        
handler.postDelayed(
            new Runnable() {
                public void run() {
                   //  Do your own code
                }
            }, 1000L);