我正在尝试创建一个应用程序,该应用程序会定期自动拨打电话到指定的号码。我为此目的使用了Alarm Manager和Broadcast Receiver。警报管理器无法启动PHONE CALL活动,应用程序终止会发出错误。
这是我的代码。我是这个Dev的新手。
* MainActivity.java
#!/bin/bash
#Backup:
asbackup -h localhost -n test -d /home/asif/aerobckups -r
#Restore:
asrestore -d /home/asif/aerobckups
* AlarmReceiver.java
0 0 * * 4 /home/ramesh/backup.sh
我收到错误"来电需要用户许可...." 在
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TextView label;
private EditText phoneNum;
private PendingIntent pendingIntent;
private AlarmManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
label=(TextView)findViewById(R.id.entertextlabel);
phoneNum=(EditText)findViewById(R.id.phonenofield);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
//pendingIntent = PendingIntent.getActivity(this,1,alarmIntent,0);
}
public void startAlarm(View view) {
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancelAlarm(View view) {
if (manager != null) {
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
}
}
* mainactivity.xml
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
/**
* Created by Saud on 05/11/2015.
*/
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent arg1) {
// For our recurring task, we'll just display a message
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
Intent in = new Intent(Intent.ACTION_CALL);
in.setData(Uri.parse("tel:03324310929"));
context.startActivity(in);
}
}
* Mantifest.xml
context.startActivity(in);
答案 0 :(得分:0)
问题已解决。
我使用了Flag" FLAG_ACTIVITY_NEW_TASK"具有AlarmReceiver类的意图。
新代码如下
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
/**
* Created by Saud on 05/11/2015.
*/
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent arg1) {
// For our recurring task, we'll just display a message
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
// Intent in = new Intent(Intent.ACTION_CALL);
//in.setData(Uri.parse("tel:03324310929"));
Intent in = new Intent(Intent.ACTION_CALL);
in.setData(Uri.parse("tel:03324310929"));
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in);
}
}