我的服务正常运行,但出于某种原因,它不会启动我的活动。它也没有在logcat中显示错误。我希望我的服务能够开始另一项活动。那可能吗?我检查以确保我的if语句是正确的,它是。它打印出HELLO。
以下服务代码:
package com.example.textsmslock;
import java.util.Iterator;
import java.util.List;
import android.app.ActivityManager;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.util.Log;
public class Logs extends Service
{
@Override
public IBinder onBind(Intent arg0){
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
System.out.println("LOGS STARTED");
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext())
{
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try
{
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
//System.out.println("this is the charsequence: " + c);
//System.out.println("this is the log: " + Log.w("LABEL", c.toString()));
if(c.equals("Messaging"))
{
System.out.println("HELLO \n");
startActivity(new intent(("com.example.textsmslock.Enable"));
}
}
catch(Exception e)
{
//Name Not FOund Exception
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("LOGS STOPPED");
}
}
android manifest
<activity
android:name=".Enable"
android:label="@string/title_activity_enable" >
<intent-filter>
<action android:name="com.example.textsmslock.Enable" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service android:name=".Logs" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</service>
答案 0 :(得分:0)
您必须将标记Intent.FLAG_ACTIVITY_NEW_TASK添加到活动意图。
这一行
startActivity(new intent(("com.example.textsmslock.Enable"));
现在应该是
Intent i = new Intent( this, YourActivity.class );
i.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity( i );