New sample for Android local notifications (#651)

This commit is contained in:
Mark McLemore 2015-07-23 17:58:14 -07:00
Родитель 9f10795337
Коммит a7076cd979
25 изменённых файлов: 424 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<SampleMetadata>
<ID>51B3A4D9-5D1A-4D9C-8E36-CA09A4D3A050</ID>
<IsFullApplication>false</IsFullApplication>
<Level>Intermediate</Level>
<Tags>Notifications</Tags>
<SupportedPlatforms>Android</SupportedPlatforms>
<Gallery>false</Gallery>
<LicenseRequirement>Indie</LicenseRequirement>
</SampleMetadata>

Просмотреть файл

@ -0,0 +1,19 @@
Any raw assets you want to be deployed with your application can be placed in
this directory (and child directories) and given a Build Action of "AndroidAsset".
These files will be deployed with you package and will be accessible using Android's
AssetManager, like this:
public class ReadAsset : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
InputStream input = Assets.Open ("my_asset.txt");
}
}
Additionally, some Android functions will automatically load asset files:
Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");

Просмотреть файл

@ -0,0 +1,70 @@
using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using Java.Lang;
using String = System.String;
using NotificationCompat = Android.Support.V4.App.NotificationCompat;
using TaskStackBuilder = Android.Support.V4.App.TaskStackBuilder;
namespace Notifications
{
[Activity(Label = "Notifications", MainLauncher = true, Icon = "@drawable/Icon")]
public class MainActivity : Activity
{
// Unique ID for our notification:
private static readonly int ButtonClickNotificationId = 1000;
// Number of times the button is tapped (starts with first tap):
private int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
// Display the "Hello World, Click Me!" button and register its event handler:
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += ButtonOnClick;
}
// Handler for button click events.
private void ButtonOnClick(object sender, EventArgs eventArgs)
{
// Pass the current button press count value to the next activity:
Bundle valuesForActivity = new Bundle();
valuesForActivity.PutInt("count", count);
// When the user clicks the notification, SecondActivity will start up.
Intent resultIntent = new Intent(this, typeof(SecondActivity));
// Pass some values to SecondActivity:
resultIntent.PutExtras(valuesForActivity);
// Construct a back stack for cross-task navigation:
TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
stackBuilder.AddParentStack(Class.FromType(typeof(SecondActivity)));
stackBuilder.AddNextIntent(resultIntent);
// Create the PendingIntent with the back stack:
PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
// Build the notification:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it
.SetContentIntent(resultPendingIntent) // Start up this activity when the user clicks the intent.
.SetContentTitle("Button Clicked") // Set the title
.SetNumber(count) // Display the count in the Content Info
.SetSmallIcon(Resource.Drawable.ic_stat_button_click) // This is the icon to display
.SetContentText(String.Format("The button has been clicked {0} times.", count)); // the message to display.
// Finally, publish the notification:
NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(ButtonClickNotificationId, builder.Build());
// Increment the button press count:
count++;
}
}
}

Просмотреть файл

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{EC6A0616-67F3-46DC-817B-01B9866D283C}</ProjectGuid>
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Notifications</RootNamespace>
<AssemblyName>Notifications</AssemblyName>
<FileAlignment>512</FileAlignment>
<AndroidApplication>true</AndroidApplication>
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<TargetFrameworkVersion>v4.4</TargetFrameworkVersion>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<AndroidUseLatestPlatformSdk>False</AndroidUseLatestPlatformSdk>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>False</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidLinkMode>None</AndroidLinkMode>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>True</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
</PropertyGroup>
<ItemGroup>
<Reference Include="Mono.Android" />
<Reference Include="mscorlib" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Xml" />
<Reference Include="Xamarin.Android.Support.v4">
<HintPath>..\packages\Xamarin.Android.Support.v4.22.2.0.0\lib\MonoAndroid403\Xamarin.Android.Support.v4.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="MainActivity.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SecondActivity.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="Resources\AboutResources.txt" />
<None Include="Assets\AboutAssets.txt" />
<AndroidResource Include="Resources\Layout\Second.axml">
<SubType>AndroidResource</SubType>
</AndroidResource>
<AndroidResource Include="Resources\drawable-xxhdpi\Icon.png" />
<AndroidResource Include="Resources\drawable-hdpi\Icon.png" />
<AndroidResource Include="Resources\drawable-mdpi\Icon.png" />
<AndroidResource Include="Resources\drawable-xhdpi\Icon.png" />
<AndroidResource Include="Resources\Drawable\Icon.png" />
<None Include="Properties\AndroidManifest.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\Layout\Main.axml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\Values\Strings.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-hdpi\ic_stat_button_click.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-mdpi\ic_stat_button_click.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-xhdpi\ic_stat_button_click.png" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<ItemGroup />
</Project>

Просмотреть файл

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="Notifications.Android">
<uses-sdk />
<application android:label="Notifications.Android"></application>
</manifest>

Просмотреть файл

@ -0,0 +1,32 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Android.App;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Notifications")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Notifications")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: Application(Icon="@drawable/Icon")]

Просмотреть файл

@ -0,0 +1,50 @@
Images, layout descriptions, binary blobs and string dictionaries can be included
in your application as resource files. Various Android APIs are designed to
operate on the resource IDs instead of dealing with images, strings or binary blobs
directly.
For example, a sample Android app that contains a user interface layout (main.xml),
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
would keep its resources in the "Resources" directory of the application:
Resources/
drawable-hdpi/
icon.png
drawable-ldpi/
icon.png
drawable-mdpi/
icon.png
layout/
main.xml
values/
strings.xml
In order to get the build system to recognize Android resources, set the build action to
"AndroidResource". The native Android APIs do not operate directly with filenames, but
instead operate on resource IDs. When you compile an Android application that uses resources,
the build system will package the resources for distribution and generate a class called
"Resource" that contains the tokens for each one of the resources included. For example,
for the above Resources layout, this is what the Resource class would expose:
public class Resource {
public class drawable {
public const int icon = 0x123;
}
public class layout {
public const int main = 0x456;
}
public class strings {
public const int first_string = 0xabc;
public const int second_string = 0xbcd;
}
}
You would then use R.drawable.icon to reference the drawable/icon.png file, or Resource.layout.main
to reference the layout/main.xml file, or Resource.strings.first_string to reference the first
string in the dictionary file values/strings.xml.

Просмотреть файл

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Hello"
/>
</LinearLayout>

Просмотреть файл

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
</LinearLayout>

Просмотреть файл

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Button clicked!"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
</LinearLayout>

Просмотреть файл

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="Hello">Hello World, Click Me!</string>
<string name="ApplicationName">Notifications</string>
</resources>

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 2.3 KiB

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 1.6 KiB

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 1.6 KiB

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 1.0 KiB

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 4.5 KiB

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 2.3 KiB

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 7.1 KiB

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 9.5 KiB

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 1.6 KiB

Просмотреть файл

@ -0,0 +1,30 @@
using System;
using Android.App;
using Android.OS;
using Android.Widget;
namespace Notifications
{
[Activity(Label = "Second Activity")]
public class SecondActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Get the count value passed to us from MainActivity:
int count = Intent.Extras.GetInt("count", -1);
// No count was passed? Then just return.
if (count <= 0)
{
return;
}
// Display the count sent from the first activity:
SetContentView(Resource.Layout.Second);
TextView txtView = FindViewById<TextView>(Resource.Id.textView1);
txtView.Text = String.Format("You clicked the button {0} times in the previous activity.", count);
}
}
}

Просмотреть файл

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Xamarin.Android.Support.v4" version="22.2.0.0" targetFramework="MonoAndroid44" />
</packages>

Просмотреть файл

@ -0,0 +1,50 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Notifications.Android", "Notifications.Android\Notifications.Android.csproj", "{EC6A0616-67F3-46DC-817B-01B9866D283C}"
EndProject
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
Ad-Hoc|Mixed Platforms = Ad-Hoc|Mixed Platforms
AppStore|Any CPU = AppStore|Any CPU
AppStore|Mixed Platforms = AppStore|Mixed Platforms
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Release|Any CPU = Release|Any CPU
Release|Mixed Platforms = Release|Mixed Platforms
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Ad-Hoc|Any CPU.ActiveCfg = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Ad-Hoc|Any CPU.Build.0 = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Ad-Hoc|Any CPU.Deploy.0 = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Ad-Hoc|Mixed Platforms.ActiveCfg = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Ad-Hoc|Mixed Platforms.Build.0 = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Ad-Hoc|Mixed Platforms.Deploy.0 = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.AppStore|Any CPU.ActiveCfg = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.AppStore|Any CPU.Build.0 = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.AppStore|Any CPU.Deploy.0 = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.AppStore|Mixed Platforms.ActiveCfg = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.AppStore|Mixed Platforms.Build.0 = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.AppStore|Mixed Platforms.Deploy.0 = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Debug|Mixed Platforms.Deploy.0 = Debug|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Release|Any CPU.Build.0 = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Release|Any CPU.Deploy.0 = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{EC6A0616-67F3-46DC-817B-01B9866D283C}.Release|Mixed Platforms.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Notifications.Android\Notifications.Android.csproj
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

Просмотреть файл

@ -0,0 +1,12 @@
Android Local Notifications Sample
==================================
This sample app accompanies the article,
[Walkthrough - Using Local Notifications in Xamarin.Android](http://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/local_notifications_in_android_walkthrough/).
![](Screenshots/screenshots.png)
When you tap the button displayed in the MainActivity screen, a
notification is created. When you tap the notification, it
takes you to a SecondActivity screen.

Двоичные данные
LocalNotifications/Screenshots/screenshots.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 144 KiB