Sunday, November 15, 2015

How to disable Activity and enable it programmatically?

In this example there are two activities declared in AndroidManifest.xml file. SetupActivity is enabled and MainActivity is disabled by default.

<activity
      android:name=”.SetupActivity”
      android:label=”@string/app_name_setup”
      android:icon=”@drawable/app_setup_icon”
      android:enabled=”true”>
         <intent-filter>
             <action android:name=”android.intent.action.MAIN”/>
             <category android:name=”android.intent.category.LAUNCHER”/>
         </intent-filter>
</activity>
<activity
      android:name=”.MainActivity”
      android:label=”@string/app_name”
      android:icon=”@string/app_icon”
      android:enabled=”false”>
         <intent-filter>
             <action android:name=”android.intent.action.MAIN”/>
             <category android:name=”android.intent.category.LAUNCHER”/>
         </intent-filter>
</activity>

You can enable MainActivity by using following code.

private void toggleActivities() {
     PackageManager packageManager = getPackageManager();
     // Enable the main activity
     packageManager.setComponentEnabledSetting(new ComponentName(this, MainActivity.class),
     PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
     // Disable the setup activity
     packageManager.setComponentEnabledSetting(new ComponentName(this, SetupActivity.class),
     PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

Sometimes you have a component, usually a Service, that you don’t want to expose to the rest of the system for security purposes. To do this, you can set the android:exported attribute to false, which will effectively hide that component from the rest of the system.

Thursday, October 1, 2015

How to inspect the values inside the route in ASP.Net MVC C#

public ActionResult Index()
{

    var Controller = RouteData.Values["controller"];

    var Action = RouteData.Values["action"];

    var Id = RouteData.Values["id"];

    string Output = string.Format("Controller = {0}, Action= {1}, Id= {2}",
               Controller, Action, Id);
    ViewBag.Message = Output;

    return View();
}

Wednesday, July 15, 2015

How to check system installation date in Windows computer?

If you want to check windows installation date, go to command prompt and type the following command.

systeminfo | find/i "install date"

And you will get something like this.




Tuesday, July 14, 2015

How use getContentResolver in a class without activity?

In android development, methods like getContentResolver() need context to call these kinds of methods. But some classes does not have  Context. In those cases it is used like this.
  • Application: getApplicationContext() 
  • Activity: this (as Activity extends Context) 
  • Fragment: getActivity()

Saturday, June 20, 2015

HTTP Status 405 - HTTP method GET is not supported by this URL

When I'm working with Java Servlet, I encountered "HTTP Status 405" error. But code seems to legit. The problem was, servlet doGet method calling super method.

package uk.co.andromedatech.onlineradio;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Test extends HttpServlet{

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
resp.setContentType("text/html");

PrintWriter out = resp.getWriter();
out.println("Leann....");
}

}

After removal of red marked code selection, servlet compiled and started to work.

Sunday, October 26, 2014

C# - List all the links in a website.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Text.RegularExpressions;

namespace ProgrammingDotNet
{
    class Program
    {
        public static void Main(string[] args)
        {
            StreamReader reader = null;
            try
            {
                WebRequest request = WebRequest.Create("http://www.techexams.net/");
                WebResponse response = request.GetResponse();
                reader = new StreamReader(response.GetResponseStream());
                string content = reader.ReadToEnd();

                Regex regex = new Regex("href\\s*=\\s*\"([^\"]*)\"", RegexOptions.IgnoreCase);

                MatchCollection matches = regex.Matches(content);
                foreach (Match match in matches)
                {
                    Console.WriteLine(match.Groups[1]);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            Console.ReadLine();
        }
    }
}

C# - Simple program that dumps text files to the windows console.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ProgrammingDotNet
{
    class Program
    {
        public static void Main(string[] args)
        {
            String filename = "C:\\Users\\Isuru\\Desktop\\google_5000000.txt";
            StreamReader reader = new StreamReader(filename);
            for (String line = reader.ReadLine(); line != null; line = reader.ReadLine())
            {
                Console.WriteLine(line);
            }
            reader.Close();
            Console.ReadLine();
        }
    }
}