2013-09-23 107 views
0

我想送無需用戶干預短信發送後,在後臺短信,所以我裝箱一個BroadcastReceiver來解決這個問題。此BroadcastReceiver將在啓動時收到"BOOT_COMPLETED"通知。在OnReceive函數中,我們可以發送短信(啓動5分鐘後)。我試過這個邏輯,但它不工作。第一次啓動

我是從在這個網站上的Android 3.1+後,我們不能用這樣的邏輯來滿足這個要求一些其他職位就知道了。但是我在Android 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> 

回答

0

您必須創建一個服務,它將在5分鐘後發送短信。

在Broadcastreceiver中調用您的服務。

  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()方法中發送短信。

+0

我試過這個廣播接收器不會在Android 3.0/3.1版本啓動後被調用。那麼你能否建議其他方式? – user2806748

+0

@ user2806748你可以請你發佈你的代碼,以便你完成。 – Ram

+0

我已添加代碼。你現在可以引導我嗎?謝謝 – user2806748

0

你應該從你的(BOOT_COMPLETED)廣播接收器您的onReceive方法啓動服務。

在這裏看到:

http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html#startingservices_alarmmanager

還要注意:

如果應用程序安裝在SD卡上,那麼它是不是可用的android.intent.action.BOOT_COMPLETED事件之後。在這種情況下注冊您的android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE事件。

另外請注意,由於Android 3.0的用戶必須在你的應用程序可以接收android.intent.action.BOOT_COMPLETED事件之前至少一次啓動的應用程序。

+0

感謝您的建議,但在我的要求我不希望用戶手動啓動我的應用程序。這是一種銷售跟蹤器應用程序,所以當用戶第一次啓動手機時,SMS應該在後臺發送。你能否建議一些其他方式來達到這個要求? – user2806748

+0

我想你可能會走運。沒有安裝應用程序的時間,爲了在BOOT_COMPLETED上運行,至少需要運行一次應用程序。 http://developer.android.com/about/versions/android-3.1.html#launchcontrols – Damian

+0

我還有一個要求。一旦發送短信,我們需要存儲這些信息,以便當用戶重新啓動手機時,不應發送短信。我試圖爲此目的,我們可以使用「SharedPreferenceS」,但這些只能在Actitvity類中使用,而在這種情況下我沒有使用任何活動類?那麼,我該如何儲存這些設置?請幫幫我。非常感謝。 – user2806748

0

使用掛起的意圖,5分鐘的開機後報警管理器發送消息。

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); 
在Shedulesms.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

+0

@ Virendra,謝謝你的建議。但是如何在第一次啓動後調用這段代碼。我註冊了廣播接收器來接收BOOTUP_COMPLETE事件,但它在3.1版之後不起作用。請糾正我,如果我錯過了一些東西,並在這方面指導我。謝謝 – user2806748

0

Trying to start a service on boot on Android,有的在一些答案中描述手機有一個fastboot選項。要支持這些電話,請將

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

添加到您的意圖過濾器。