Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 首次启动后在后台发送SMS_Android_Broadcastreceiver - Fatal编程技术网

Android 首次启动后在后台发送SMS

Android 首次启动后在后台发送SMS,android,broadcastreceiver,Android,Broadcastreceiver,我想在没有用户干预的情况下发送短信,所以我装了一个广播接收器来解决这个问题。此BroadcastReceiver将在启动时收到“启动完成”通知。在OnReceive功能中,我们可以发送SMS(启动5分钟后)。我试过这个逻辑,但它不起作用 我从这个网站上的其他帖子中了解到,在安卓3.1+之后,我们无法使用这种逻辑来满足这一要求。但我在安卓2.3上尝试了这种逻辑,它似乎也不起作用 请向我建议如何在Android 2.3和Android 4.0+版本上解决此问题。我不想为这个需求创建任何UI 我正在尝

我想在没有用户干预的情况下发送短信,所以我装了一个
广播接收器来解决这个问题。此
BroadcastReceiver
将在启动时收到
“启动完成”
通知。在
OnReceive
功能中,我们可以发送SMS(启动5分钟后)。我试过这个逻辑,但它不起作用

我从这个网站上的其他帖子中了解到,在安卓3.1+之后,我们无法使用这种逻辑来满足这一要求。但我在安卓2.3上尝试了这种逻辑,它似乎也不起作用

请向我建议如何在Android 2.3和Android 4.0+版本上解决此问题。我不想为这个需求创建任何UI

我正在尝试下面的代码

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.telephony.SmsManager;
import android.util.Log;

class MyCountDownTimer extends CountDownTimer {
private int no_of_attempts;
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
no_of_attempts = 0;
}

@Override
public void onFinish() {
    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("+9198104084753", null, "Test Message ", null, null);
      } catch (Exception e) {
        Log.d("BGSMS","SMS Sending failed..Try to resend SMS");
        no_of_attempts++;
        if (no_of_attempts <= 3)
        {
            long startTime = 5 * 1000;
            long interval = 1 * 1000;
            CountDownTimer countDownTimer;
            countDownTimer = new MyCountDownTimer(startTime, interval);
            countDownTimer.start();
        }
      }

}

@Override
public void onTick(long millisUntilFinished) {

}
}

public class SalestrackerReceiver extends BroadcastReceiver {
    private CountDownTimer countDownTimer;
    private final long startTime = 5 * 1000;
    private final long interval = 1 * 1000;
  @Override
  public void onReceive(Context context, Intent intent) {
      Log.v("Anshul","Anshul Broadcast receiver received");
        countDownTimer = new MyCountDownTimer(startTime, interval);
        countDownTimer.start();
}
}

Manifest File :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sales_tracker"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.SEND_SMS" />

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="SalestrackerReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.CountDownTimer;
导入android.telephony.smsmsmanager;
导入android.util.Log;
类MyCountDownTimer扩展了CountDownTimer{
私人int无任何尝试;
公共MyCountDownTimer(长启动时间、长间隔){
超级(开始时间、间隔);
没有尝试的次数=0;
}
@凌驾
公共无效onFinish(){
试一试{
SmsManager SmsManager=smsmsmanager.getDefault();
smsManager.sendTextMessage(“+9198104084753”,null,“测试消息”,null,null);
}捕获(例外e){
Log.d(“BGSMS”,“短信发送失败..尝试重新发送短信”);
没有任何尝试++;

如果(没有任何尝试您必须创建一个服务,该服务将在5分钟后发送短信

在广播接收机呼叫您的服务

          public class MyReceiver extends BroadcastReceiver {

         public void onReceive(Context context, Intent intent) {

              TimerTask hourlyTask = new TimerTask () {
        @Override
             public void run () {
              Intent myIntent = new Intent(context,MyService.class); 
             startService(myIntent);
                }
               };

    // schedule the task to run starting now and then every hour...
         timer.schedule (hourlyTask,5000 * 60 * 1);
       }


    }
   }

在myservice类中,您可以编写代码,在Onstart()方法中发送sms。

您应该从(启动完成)BroadcastReceiver中的onReceive方法启动服务

请看这里:

另请注意:

如果您的应用程序安装在SD卡上,则在android.intent.action.BOOT_完成事件后,该应用程序将不可用。在本例中,请注册android.intent.action.action_EXTERNAL_APPLICATIONS_available事件


另请注意,从Android 3.0开始,用户需要至少启动应用程序一次,应用程序才能接收Android.intent.action.BOOT_完成的事件。

启动5分钟后,使用pending intent and Alarm Manager发送消息

Intent i = new Intent(this,Shedulesms.class);
Bundle msg = new Bundle();
msg .putString("number", "1234567890");
msg .putString("message", "This is test message");
i.putExtras(msg );
PendingIntent pu=PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
Calendar cal= Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.MINUTE,cal.get(Calendar.MINUTE)+5);
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pu);
在schedulesms.java中

enter code here

SmsManager sms=SmsManager.getDefault();
Bundle b=getIntent().getExtras();
String number=b.getString("number");
String msg=b.getString("message");
sms.sendTextMessage(number, null,msg, null, null);
在android manifiest文件中授予权限

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


并在manifiest中注册广播接收器

,如中的一些答案所述,有些手机具有快速启动选项。要支持这些手机,请添加

<action android:name="android.intent.action.QUICKBOOT_POWERON" />


到您的意向过滤器。

谢谢您的建议,但在我的要求中,我不希望用户手动启动我的应用程序。这是一种销售跟踪应用程序,所以当用户第一次启动手机时,短信应该在后台发送。您能建议其他方法来实现此要求吗?我想您可能运气不好。没有挂钩当你的应用程序安装完毕,并且为了在启动完成后执行操作,你需要至少运行一次你的应用程序。我还有一个要求。发送短信后,我们需要存储此信息,以便当用户重新启动手机时,不应发送短信。我检查过,为此目的,我们可以使用“SharedReferences”但是这些只能在activity类中使用,而在这种情况下我没有使用任何activity类?那么我应该怎么做来存储这些设置呢?请帮助我。非常感谢。我尝试过在Android 3.0/3.1版本启动时不会调用此广播接收器。所以,你能建议其他方法吗?@user2806748给出Hi的完整路径。。不w它在安卓2.3上运行良好,但在安卓4.0+版本上似乎不起作用。yu能否建议我应该做些什么更改,以便它也能在安卓4.0+版本上运行。非常感谢您的帮助。我还有一个要求。发送短信后,我们需要存储这些信息,以便用户重新启动手机时,再发送短信不应发送。我检查过,为此,我们可以使用“SharedReferences”但是这些只能在activity类中使用,而在这种情况下我没有使用任何activity类?那么我应该如何存储这些设置呢?请帮助我。非常感谢。@user2806748您可以在receiver类中使用shared pref.。SharedReferences prefs=context.GetSharedReferences(“.*.*”,context.MODE\U PRIVATE)@Virendra,谢谢你的建议。但是在第一次启动后如何调用此代码。我注册了广播接收器以接收BOOTUP_COMPLETE事件,但在3.1版本后它不工作。如果我遗漏了什么,请纠正我,并在这方面指导我。谢谢