使用计时器定期拨打电话?

时间:2014-01-08 12:48:31

标签: android timertask

我无法使用计时器内的电话管理器拨打电话(scheduleAtFixedRate) 在android中。我收到错误就像你不能在计时器内创建处理程序。

是否有其他方法可以定期拨打电话。

1 个答案:

答案 0 :(得分:1)

请逐步执行此操作

在你的AndroidManifest.XML中写下这个

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

之后创建一个后台服务,它将在24小时后每天拨打电话

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();

    }

}

为此,请再次将AndroidManifest.xml标记在application标记内

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

之后从活动中调用服务

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);

        }

        });

    }

}

除了xml布局外,我给你所有测试的源代码。