Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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_Broadcastreceiver - Fatal编程技术网

Android 正在从服务启动已在运行的活动

Android 正在从服务启动已在运行的活动,android,android-intent,service,broadcastreceiver,Android,Android Intent,Service,Broadcastreceiver,我正在尝试制作锁屏,所以我在它里面制作了服务类n广播接收器和MainActivity。 它一直工作到屏幕关闭,whn屏幕打开时主要活动关闭并显示异常。 请帮我解决它 MyService.java package com.example.broadcast_receiver; import android.app.Service; ... public class MyService extends Service { @Override public IBinder onBi

我正在尝试制作锁屏,所以我在它里面制作了服务类n广播接收器和MainActivity。 它一直工作到屏幕关闭,whn屏幕打开时主要活动关闭并显示异常。 请帮我解决它

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 (未添加用户权限)



请帮助我解决dis问题。

错误告诉您,如果不设置
意图标志NEW\u TASK
,您就无法从
活动
外部调用
startActivity()
。在调用
startActivity()之前,将
Intent.FLAG\u ACTIVITY\u NEW\u TASK
添加到
Intent


发布您的LogCat错误。另外,什么是
上下文
?一个服务有它自己的
上下文
,所以你应该能够使用
MyService。这个
@TronicZomB u可以从下面的链接看到LogCat@codeMagic我没有使用新上下文,我只使用服务上下文,检查我是否将服务上下文名称作为上下文。Thnx,我jst从另一个链接找到了dis解决方案,即。
 <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>
Intent i=new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);