Q:

How to add background only to title frame and an other only to the last frame?

I just want to add a background only in the beamer title page. I can add it in the .tex as follows:
\setbeamertemplate{background canvas}{
    \includegraphics[width=\paperwidth,height=\paperheight]{img/bg-first.pdf}
}

But I don't want the above command in the .tex, but in the .sty files. Thanks for pointing me out how I can add the above command in the .sty files.
Please find the .sty and .tex files below (all of them are quite simple).

main.sty
\mode<presentation>

\DeclareOptionBeamer{titlepage}{\PassOptionsToPackage{titlepage=#1}{beamertheme-Inner}}
\ProcessOptionsBeamer

\usetheme{Inner}
\usetheme{Outer}

\setbeamertemplate{navigation symbols}{}

\mode
<all>

inner.sty
\mode<presentation>

\DeclareOptionBeamer{titlepage}{\def\beamer@titlepage{#1}}

\defbeamertemplate*{titlepage}
{
  \begin{center}
    \usebeamerfont{title}\inserttitle
    \ifx\insertsubtitle\@empty
    \else
      \vskip2ex
      \usebeamerfont{subtitle}\insertsubtitle
    \fi
    \ifx\insertauthor\@empty
    \else
      \vskip2ex
      \usebeamerfont{author}\insertauthor
    \fi
    \ifx\insertdate\@empty
    \else
      \vskip2ex
      \usebeamerfont{date}\insertdate%
    \fi
  \end{center}
  \vfill
}

.tex file
\usetheme{Afnic}

\title{An example theme for \LaTeX{} Beamer}
\author{Sandoche}
\date{February 29, 2012}

\begin{document}

\begin{frame}[plain]
  \maketitle
\end{frame}

\AtBeginSection[]
{
   \begin{frame}
       \frametitle{Outline}
       \tableofcontents[currentsection]
   \end{frame}
}

A:

You could check for the frame number:
\setbeamertemplate{background canvas}{%
    \ifnum\c@framenumber=1%
        % On title page
    \fi%
}

You may extend this to set thing to all frames except the title:
\setbeamertemplate{background canvas}{%
    \ifnum\c@framenumber=1%
        % On title page
    \else%
            % Other frames background
    \fi%
}

Extended version
As requested a version that also checks for the last frame. We use etoolbox to enable nested if statements.
\documentclass{beamer}

\usepackage{etoolbox}

\makeatletter

\setbeamertemplate{background canvas}{%
   \ifnumequal{\c@framenumber}{1}{%
      % First frame
      \color{red!50}\rule{\paperwidth}{\paperheight}
   }{%
      \ifnumequal{\c@framenumber}{\inserttotalframenumber}{
         % Last frame
         \color{orange!50}\rule{\paperwidth}{\paperheight}
      }{%
         % Other frames
         \color{green!50}\rule{\paperwidth}{\paperheight}
      }%
   }%
}

\makeatother

\begin{document}
\frame{1}

\frame{2}

\frame{3}

\frame{4}
\end{document}

\c@framenumber is the counter of the current frame and \inserttotalframenumber holds the number of the last frame. Certanly one can omit the Other frames part

A:

The solution of Tobi has some significant disadvantages. It only works when the titlepage is actually the first frame and also produces a problem when not using the titlepage. A somewhat better solution is using the \AddToShipoutPictureFG* command of eso-pic inside the \setbeamertemplate{title page} command. So the code for the askers inner.sty would be:
\setbeamertemplate{title page}
{
  \AddToShipoutPictureFG*{\includegraphics[width=\paperwidth,height=\paperheight]{img/bg-first.pdf}}
  \begin{center}
    \usebeamerfont{title}\inserttitle
    \ifx\insertsubtitle\@empty
    \else
      \vskip2ex
      \usebeamerfont{subtitle}\insertsubtitle
    \fi
    \ifx\insertauthor\@empty
    \else
      \vskip2ex
      \usebeamerfont{author}\insertauthor
    \fi
    \ifx\insertdate\@empty
    \else
      \vskip2ex
      \usebeamerfont{date}\insertdate%
    \fi
  \end{center}
  \vfill
}

