2013-04-23 54 views
0

我想鎖定屏幕,所以我在它的broadcastReceiver和MainActivity中創建了Service類n。 其工作到屏幕關閉,當屏幕當時主要活動關閉並顯示異常。 請幫我解決它。從服務啓動已經運行的活動

MyService.java

package com.example.broadcast_receiver; 
import android.app.Service; 
... 

public class MyService extends Service { 

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

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     Log.i("[myService]", "onCreate"); 
     registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON)); 
     registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF)); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
     Log.i("[myService]", "onStart"); 
    } 

    @Override 
    public void onDestroy() { 
     Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show(); 
     Log.i("[myService]", "onDestroy"); 
    } 

    BroadcastReceiver mybroadcast = new BroadcastReceiver(){ 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      // TODO Auto-generated method stub 
      Log.i("[BroadcastReceiver]", "MyReceiver"); 

      if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){ 
       Log.i("[BroadcastReceiver]", "Screen ON"); 
       Intent i=new Intent(context, MainActivity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 
       context.startActivity(i); 

      } 
      else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){ 
       Log.i("[BroadcastReceiver]", "Screen OFF"); 
      } 
     } 

    }; 
} 

MainActivity.java

package com.example.broadcast_receiver; 
import android.os.Bundle; 
... 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Log.i("[MainActivity]", "Created"); 

     try{ 
     boolean a=isMyServiceRunning(); 
     if(a==false){ 
      startService(new Intent(this,MyService.class)); 
     } 
     }catch (Exception e) { 
      // TODO: handle exception 
      Log.i("Error", e.getMessage().toString()); 
      Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 

    private boolean isMyServiceRunning() { 
     ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
     for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { 
      if (MyService.class.getName().equals(service.service.getClassName())) { 
       return true; 
      } 
     } 
     return false; 
    } 
} 

AndroidManifest.xml中 (無用戶權限添加)

<application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <service android:name="com.example.broadcast_receiver.MyService" 
      android:label="@string/app_name" 
      android:icon="@drawable/ic_launcher" ></service> 

     <activity 
      android:name="com.example.broadcast_receiver.MainActivity" 
      android:label="@string/app_name" 
      android:launchMode="singleTop" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

請幫我解決DIS問題。

+1

安置自己的logcat錯誤之前添加Intent.FLAG_ACTIVITY_NEW_TASK到您Intent。 – TronicZomB 2013-04-23 14:43:40

+0

另外,什麼是'上下文'?一個服務有它自己的'context',所以你應該可以使用'MyService.this' – codeMagic 2013-04-23 14:46:53

+0

@TronicZomB你可以從下面看到LogCat鏈接 https://dl.dropboxusercontent.com/u/6608612/log.JPG – user2290872 2013-04-23 17:20:26

回答

0

錯誤告訴你,如果不設置Intent flag NEW_TASK,則不能從Activity以外的地址呼叫startActivity()。調用startActivity()

Intent i=new Intent(context, MainActivity.class); 
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(i); 
+0

Thnx,我從其他鏈接找到解決方案,即 http://stackoverflow.com/questions/15414890/launching-already-running-activity-from-service-intent – user2290872 2013-04-23 17:30:36

+0

不客氣。很高興我能幫上忙 – codeMagic 2013-04-23 17:31:08

相關問題