Вопрос

I am not able to make a call using the telephone manager inside the timer (scheduleAtFixedRate) in android. I am getting error like you cannot create handler inside the timer.

Is there any other method to make calls at regular interval of time.

Это было полезно?

Решение

Please do this step by step manner

in your AndroidManifest.XML write this

 <uses-permission android:name="android.permission.CALL_PHONE"/>
 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

After that create a background Service which will call the phone everyday after 24 hours

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;

public class MyService extends Service {

    MediaPlayer mp; 
    @Override   
    public IBinder onBind(Intent arg0) {

        return null;

    }

    @Override   
    public void onCreate() 
    {   
      super.onCreate(); 


    }

    @Override   
    public int onStartCommand(Intent intent, int flags, int startId) 
    {   


        String num="98XXXX51";

        Intent in = new Intent(Intent.ACTION_CALL);
        in.setData(Uri.parse("tel:" + num));
        in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        in.addFlags(Intent.FLAG_FROM_BACKGROUND);
        startActivity(in);


        return 0;

    }

    @Override   
    public void onDestroy() 
    {   
        mp.release();       
        super.onDestroy();

    }

}

For this again paste the in AndroidManifest.xml within application tag

 <service
        android:name=".MyService"
        android:enabled="true" >
    </service>

After that call the service from activity

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;

import android.content.Context;
import android.content.Intent;

import android.view.View;
import android.widget.Button;


public class MainActivity1 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button play, stop;

        play = (Button) findViewById(R.id.playId);      
        stop = (Button) findViewById(R.id.stopId);      
        play.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            Calendar cal = Calendar.getInstance();

            Intent name = new Intent(MainActivity1.this, MyService.class);          
            PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0, name, 0);//.getService(this, 0, intent, 0);

            AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            // Start every 30 seconds
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pintent);
            //alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);
            startService(new Intent(getBaseContext(), MyService.class));

        }

        });

        stop.setOnClickListener(new View.OnClickListener() {

        @Override

          public void onClick(View v) {

        Intent name = new Intent(MainActivity1.this, MyService.class);

        stopService(name);

        }

        });

    }

}

I give you all tested sources except xml layout.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top