2015-02-07 81 views
0

我是新來的android和我想通過做一些基礎項目學習。我正在使用的當前應用程序要求我使用BroadcastReceiver,但我需要它在手機啓動和應用程序關閉時運行。手機開機時啓動進程,並在應用程序關閉時保持運行狀態。 Android

我需要它來接收所有傳入的短信,即使應用沒有運行。

目前我有一個startForeground使用通知來運行。有沒有辦法讓應用程序在沒有通知的情況下運行?

請幫忙。這裏是我的代碼:

public class IncomingSMS extends Service { 
    // Get the object of SmsManager 
    final SmsManager sms = SmsManager.getDefault(); 

    //get audioManager 
    AudioManager aud; 

    public void onCreate(){ 
     Log.i("MyActivity", "aaa"); 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction("android.provider.Telephony.SMS_RECEIVED"); 
     filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED); 



     registerReceiver(receiver, filter); 

registerReceiver(receiver, filter); 
    Notification notification= new Notification(); 
    startForeground(1, notification); 


    } 

    public void onDestroy(){ 
     unregisterReceiver(receiver); 
     Log.i("MyActivity", "Destroyed"); 
    } 
    public int onStartCommand(Intent intent, int flags, int startId){ 
     return START_STICKY; 
    } 


    private final BroadcastReceiver receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      // Retrieves a map of extended data from the intent. 
      Log.i("MyActivity", "aaa"); 
      final Bundle bundle = intent.getExtras(); 
      aud = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 

      try { 


       if (bundle != null) { 
        Log.i("MyActivity", "Bundle"); 

        final Object[] pdusObj = (Object[]) bundle.get("pdus"); 

        for (int i = 0; i < pdusObj.length; i++) { 

         SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]); 

         String message = currentMessage.getDisplayMessageBody(); 
         Log.i("MyActivity", message); 
         //Some Code 



        } // end for loop 
       } // bundle is null 




      }catch (Exception e) { 
       Log.e("SmsReceiver", "Exception smsReceiver" + e); 
       Log.i("MyActivity", "bbb"); 
      } 
     } 
    }; 


    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
} 

而且AndroidManifest.xml中:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="pi.sum.nesbtesh.SARA" > 

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

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <service android:name="......"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED"/> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
     </service> 


     <meta-data android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
     <activity android:name="com.google.android.gms.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
      android:theme="@android:style/Theme.Translucent" /> 
    </application> 
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> 
    <uses-permission android:name="android.permission.READ_SMS" /> 
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
</manifest> 

回答

0

你想要的是一個BroadcastRceiever在手機上啓動,here you go運行。基本上你在回調中開始你的服務。

對於應用程序關閉時,我不會說這很有可能,因爲大多數時候當你關閉一個應用程序,它只是統計在Stopped但未被破壞。所以我建議你創建一個BaseActivity這是一個Activity子類,覆蓋onStop()onPause()並以此方法啓動您的服務。

然後在你的應用程序每隔ActivityBaseActivity

+0

嗨克里斯托弗的子類,謝謝你的回答,BroadcastReciver停止啓動幾分鐘後運行。 – user3527318 2015-02-08 18:42:08

+1

BroadcastReceiver不應該運行不確定。它只接收來自android系統的廣播並執行代碼。然後,在該代碼中啓動您的服務。 – 2015-02-08 22:02:43

+0

我嘗試過,但它不起作用。永遠不是runnnig – user3527318 2015-02-09 21:29:57

相關問題