2014-11-03 111 views
0

我目前正在開發一個android應用程序,藍牙對於那個應用程序是必不可少的。我運行後臺服務。後臺服務所做的一件事是檢查藍牙是否啓用。如果不是,將會有新的通知。當你點擊它時,我希望啓用藍牙,並且用戶能夠繼續他所做的任何事情。只是。通過通知按鈕打開藍牙

現在,當我點擊通知時,它啓動一個新的活動,並啓用一個新的視圖和藍牙。我不希望這種新觀點在那裏。我在啓用藍牙後嘗試使用finish(),但是這會將我帶回到應用程序中的最後一個活動,而不是我在按通知之前使用的應用程序。

因此,我正在閱讀新聞應用程序,點擊通知和藍牙啓用,但也帶給我自己的應用程序,而不是我正在使用的新聞應用程序。

這是正在發送的新通知:

if (!mBluetoothAdapter.isEnabled()) 
{ 
      NotificationCompat.Builder mBuilder; 

      mBuilder = 
        new NotificationCompat.Builder(this) 
          .setSmallIcon(R.drawable.bluetooth) 
          .setContentTitle("Bluetooth disabled") 
          .setContentText("Click here to enable bluetooth") 
          .setOngoing(true); 
      Intent resultIntent = new Intent(this, TurnOnBluetooth.class); 
      resultIntent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 

      int mNotificationId = 159; 

      PendingIntent resultPendingIntent = 
        PendingIntent.getActivity(
          this, 
          0, 
          resultIntent, 
          PendingIntent.FLAG_UPDATE_CURRENT 
        ); 
      mBuilder.setContentIntent(resultPendingIntent); 

      mNotifyMgr.notify(mNotificationId, mBuilder.build()); 

     } 
    } 

這是TurnOnBluetooth.java中的onCreate():

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.activity_turn_on_bluetooth); 
     NotificationManager mNotifyMgr = 
       (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     mBluetoothAdapter.enable(); 
     mNotifyMgr.cancel(159); 
     finish(); 
    } 

注意,我評論的的setContentView();因爲我不想要一個新的視圖,所以我只想讓藍牙開啓並在沒有其他事情的情況下襬脫通知。完成()將我帶回到我的應用程序,而不是我在按通知之前使用的應用程序。我怎樣才能做到這一點?

回答

0

我能夠自己弄清楚這一點。我沒有開始一項新的活動,而是使用了一個可以做到這一點的廣播接收器。

廣播接收器的的onReceive():

public void onReceive(Context context, Intent intent) { 
     NotificationManager mNotifyMgr = 
       (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     mBluetoothAdapter.enable(); 
     mNotifyMgr.cancel(159); 
    } 

接收機在清單:

<receiver 
      android:name="com.novioscan.service.NotificationReceiver" 
      android:enabled="true" 
      android:exported="true" > 
      <intent-filter> 
       <action android:name="NotService"/> 
      </intent-filter> 
     </receiver> 

新通知聲明:

if(!mBluetoothAdapter.isEnabled()){ 
      NotificationCompat.Builder mBuilder; 

      mBuilder = 
        new NotificationCompat.Builder(this) 
          .setSmallIcon(R.drawable.bluetooth) 
          .setContentTitle("Bluetooth disabled") 
          .setContentText("Click here to enable bluetooth") 
          .setOngoing(true); 
      // Intent resultIntent = new Intent(this, TurnOnBluetooth.class); 
      Intent resultIntent = new Intent("NotService"); 

      //resultIntent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 

      // Sets an ID for the notification 
      int mNotificationId = 159; 

      PendingIntent resultPendingIntent = 
        PendingIntent.getBroadcast(
          this, 
          0, 
          resultIntent, 
          0 
        ); 
      mBuilder.setContentIntent(resultPendingIntent); 

      // Builds the notification and issues it. 
      mNotifyMgr.notify(mNotificationId, mBuilder.build()); 
     } 

它所有的作品真的很好現在:)