0

我的應用程序basicaly檢測到屏幕解鎖並彈出一個敬酒。它的做工精細用吐司,但是當我implent的意圖,並調用它崩潰的另一項活動..Broadcastreciever不啓動活動

package com.androidexample.screenonoff; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Service; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.IBinder; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 


public class AEScreenOnOffService extends Service { 
    BroadcastReceiver mReceiver=null; 

     @Override 
     public void onCreate() { 
      super.onCreate(); 

      // Toast.makeText(getBaseContext(), "Service on create", Toast.LENGTH_SHORT).show(); 

      // Register receiver that handles screen on and screen off logic 
      IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
      filter.addAction(Intent.ACTION_SCREEN_OFF); 
      mReceiver = new AEScreenOnOffReceiver(); 
      registerReceiver(mReceiver, filter); 

     } 

     @Override 
     public void onStart(Intent intent, int startId) { 

      boolean screenOn = false; 

      try{ 
       // Get ON/OFF values sent from receiver (AEScreenOnOffReceiver.java) 
       screenOn = intent.getBooleanExtra("screen_state", false); 

      }catch(Exception e){} 

      // Toast.makeText(getBaseContext(), "Service on start :"+screenOn, 
        //Toast.LENGTH_SHORT).show(); 

      if (!screenOn) { 

       // your code here 
       // Some time required to start any service 
       Toast.makeText(getBaseContext(), "Begin ", Toast.LENGTH_LONG).show(); 

這裏我加入了一個意圖開始活動。

Intent intent = new Intent(AEScreenOnOffService.this,postlockscreen.class); 
      startActivity(intent); 



      } else { 

       // your code here 
       // Some time required to stop any service to save battery consumption 
       //Toast.makeText(getBaseContext(), "Screen off,", Toast.LENGTH_LONG).show(); 
      } 
     } 

     @Override 
     public IBinder onBind(Intent intent) { 
      // TODO Auto-generated method stub 
      return null; 
     } 

} 

我已經檢查了新的活動類的一切似乎是罰款,但似乎沒有任何可以使用的XML ..

+0

你有沒有嘗試過的意圖? – Awadesh

+0

向我們展示此代碼發生的錯誤的logcat –

回答

0

嘗試這種方式

Intent yourintent = new Intent(this, MyActivity.class); 
      yourintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(yourintent); 

希望這有助於你

+0

是的thanx saeed ..它完全工作。謝謝一噸... –