2015-02-07 50 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

相关问题