从android中的后台服务调用屏幕锁定活动

时间:2013-08-28 07:51:21

标签: android

我正在使用服务运行计时器。我必须从保存锁定屏幕代码的服务中调用新活动。我正在使用意图从服务启动该活动。然后我收到了错误

   java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

我已在manifest文件中注册了screenlock活动。

My Service .java

 public class BgTimerService extends Service
 {

  static long second;
  static long min;
  Intent I;
  Bundle b; 
  @Override
  public void onCreate()
  {
    super.onCreate();
    showTime();     
  }
  public void showTime()
  {
    CountDownTimer countdowntimer;
    long time;

    time = (1 * 10 * 1000);
    countdowntimer = new CountDownTimer(time, 1000)
    {

        @Override
        public void onTick(long millisUntilFinished)
        {
            // TODO Auto-generated method stub

            BgTimerService.second = millisUntilFinished / 1000;
            BgTimerService.min = 0;
            while (BgTimerService.second >60)
            {
                if (BgTimerService.second > 60)
                {
                    BgTimerService.second = BgTimerService.second - 60;
                    BgTimerService.min++;

                }
            }
        }

        @Override
        public void onFinish()
        {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Hello World",
                    Toast.LENGTH_SHORT).show();
            stopSelf();
            Intent in=new Intent(BgTimerService.this,LockScreenActivity.class);
            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(in);
        }
    }.start();
}

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

LockscreenActivity.java

 public class LockScreenActivity extends DeviceAdminReceiver
 {

  public void onEnabled(Context context, Intent intent)
  {
    showToast(context, "Sample Device Admin: enabled");
  }

@Override
 public CharSequence onDisableRequested(Context context, Intent intent)
 {
    return "This is an optional message to warn the user about disabling.";
     }

@Override
public void onDisabled(Context context, Intent intent)
{
    showToast(context, "Sample Device Admin: disabled");
}

void showToast(Context context, CharSequence msg)
{
    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

}

public static class Myclass extends Activity
{

    protected PowerManager.WakeLock mWakeLock;
    DevicePolicyManager mDPM;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);

        mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        // mDPM.lockNow();

        ComponentName mAdminName = new ComponentName(Myclass.this,
                LockScreenActivity.class);


        if (!mDPM.isAdminActive(mAdminName))
        {
            Intent intent = new Intent(
                    DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                    mAdminName);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                    "is locked");
            intent.putExtra("force-locked",
                    DeviceAdminInfo.USES_POLICY_FORCE_LOCK);

            startActivityForResult(intent, 1);
            System.out
                    .println("The Device Could not lock because device admin not enabled");
            // mDPM.lockNow();
        } else
        {
            System.out.println("The Device  device admin enabled");
            Intent intent = new Intent(
                    DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                    mAdminName);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                    "onEnabled");
            mDPM.lockNow();
            mDPM.setMaximumTimeToLock(mAdminName, 0);
            intent.putExtra("force-locked",
                    DeviceAdminInfo.USES_POLICY_FORCE_LOCK);

            startActivityForResult(intent, 1);

        }

    }
}

}

我的manifest.xml是

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp.screenlock"
android:versionCode="1"
android:versionName="1.0" >
 <uses-sdk
    android:minSdkVersion="8" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".BgTimerActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".BgTimerService" />

    <activity android:name=".LockScreenActivity" >
    </activity>
    <activity
        android:name=".LockScreenActivity$Myclass"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>
    <receiver
        android:name=".LockScreenActivity"
        android:label="device_admin"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/my_admin" />
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
    </receiver>
</application>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

有谁可以弄明白我哪里出错?

0 个答案:

没有答案