0

My app - reads a text file with scanner containing mobile nos and message for each. Sends SMS to each mobile nos as per the text file. Each time no of SMS varies from 20 to 140nos which takes about 8 to 10 minutes.

If I keep the CPU running by continuous interaction with touch, all SMS are sent properly. No problem.

If I do not touch and keep the device aside, only 15 - 20 SMS are sent.

I tried to use 1 Keep screen on - not working. 2 Use partial wakelock - not working 3 Used partial wakelock with a handler for releasing wakelock after 10 min - still only 15 - 20 SMS sent but wakelock is realeased after 10 minutes. 4 Used keep screen on with partial wakelock - still same as above and not working.

Here is my code.

public class Ardtxt extends Activity {
private Button buttonsendsms;
String gg = "my app";   
File fileexists ;
String mdn, msg;
String tomdn = "9123456789";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_ardtxt);
//*********************
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    Button buttonsendsms = (Button) findViewById(R.id.buttonsendsms);


    buttonsendsms.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
            final WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "ardtxtwakelock");
            wakeLock.acquire();

            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
              @Override
              public void run() {
                  Toast.makeText(getApplicationContext(), "All SMS are sent.",Toast.LENGTH_LONG).show();
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage("9123456789", null, "wake lock released", null, null);
                    wakeLock.release();
              }
            }, 600000);


            Toast.makeText( getApplicationContext(), "start" , Toast.LENGTH_LONG).show();
            Log.d("chk","app start");

            File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"kk");
            directory.mkdirs();

            Log.d("mytxt app App", Environment.getExternalStorageDirectory()+File.separator+"kk");
             fileexists = new File(Environment.getExternalStorageDirectory()+File.separator+"kk"+File.separator, "Sample1.txt" );
               if (fileexists.exists()) {
                 //Do action
                   Log.d("app exxxxx",String.valueOf(fileexists));
                   Toast.makeText( getApplicationContext(), "subject file exists" , Toast.LENGTH_LONG).show();
                   System.out.println("file exists so can be used by us");
               Log.d("Ketan check", "Sample1.txt exists");
               try {
                   Readtxtfile();
               }catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                Log.d(gg,"Exception : file not found");
                   e.printStackTrace();
                    Toast.makeText( getApplicationContext(), "crpg file not found" , Toast.LENGTH_LONG).show();

                }
               }
        }

        });}

Pl let me know what I am missing or why partial wakelock is not keeping the CPU alive.

Thanks.

Mister Smith
  • 27,417
  • 21
  • 110
  • 193
Madiya
  • 23
  • 5
  • Did you add the pertinent wake lock permission to the manifest? If you did, then I'd try to move the SMS sending code to a thread (or AsyncTask, or better, a service with a thread) – Mister Smith Jan 23 '14 at 10:00

1 Answers1

0

Yes, I have added wakelock permission. However, I am not sure how to code a service with thread. Kindly provide a sample code if possible. Search didn't revealed any usefull code or I could not understand the same.

Thanks.