Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何让我的服务启动活动?_Android_Android Intent_Service_Android Activity - Fatal编程技术网

Android 如何让我的服务启动活动?

Android 如何让我的服务启动活动?,android,android-intent,service,android-activity,Android,Android Intent,Service,Android Activity,我的服务正在运行,但由于某些原因,它无法启动我的活动。它也不会在logcat中显示错误。我希望我的服务开始另一项活动。可能吗?我检查了一下我的陈述是否正确,是否正确。它会打印出HELLO 服务代码如下: package com.example.textsmslock; import java.util.Iterator; import java.util.List; import android.app.ActivityManager; import android.app.Service; i

我的服务正在运行,但由于某些原因,它无法启动我的活动。它也不会在logcat中显示错误。我希望我的服务开始另一项活动。可能吗?我检查了一下我的陈述是否正确,是否正确。它会打印出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"); 
       }
}
安卓清单

<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>

您必须将该标志添加到活动意图中

这条线

startActivity(new intent(("com.example.textsmslock.Enable"));
现在应该是

Intent i = new Intent( this, YourActivity.class );
i.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity( i );